我尝试splice
数组中的元素然后save()
- 似乎mongoose将整个数组发送到数据库。有没有更有效的方法来删除项目?
来自Mongoose docs中的Array.Splice:
注意:将整个数组标记为已修改,如果已保存,则会将其存储为$ set操作,可能会覆盖检索对象与保存对象之间发生的任何更改。
如果我正确地指出了这一点,那么将整个array
标记为已修改意味着将其全部通过网络发送到MongoDB服务器。
这就是我的所作所为:
roomSchema.methods.deleteFirstBoard = function(boardId) {
this.boardBucket.items.splice(1, 1);
this.save((err, result)=> {
if (err) throw err;
})
}
使用mongoose.set("debug", true)
我可以看到此查询已发送:
Mongoose: rooms.update({ _id: ObjectId("57b573cf8bdf3a61866a7032") }, { '$set': { 'boardBucket.currentBoardId': '147647695276', 'boardBucket.items': [ { boardId: '147367100952', viewPosition: '{x: 0, y: 0}', _id: ObjectId("57b573f38bdf3a61866a7039"), undoedItems: [], boardItems: [ '["Path",{"applyMatrix":true,"data":{"id":"14745566"},"segments":[[[17.5,-20],[0,0],[-2,6]],[[11.5,-2],[1.63639,-6.10919],[-3.36799,12.57383]],[[2.5,36],[3.298,-12.59236],[-4.037,15.414]],[[-10.5,82],[4.37738,-15.32082],[-1.91212,6.6924]],[[-16.5,102],[2.20101,-6.60303],[0,0]]],"strokeColor":[0.26667,0.26667,0.26667],"strokeWidth":"4"}]' ] }, { boardId: '147206474731', viewPosition: '{x: 0, y: 0}', _id: ObjectId("57b5743a8bdf3a61866a703a"), undoedItems: [], boardItems: [ '["Path",{"applyMatrix":true,"data":{"id":"14732223"},"segments":[[[203.5,-2],[0,0],[0,-6.11919]],[[212.5,-18],[-3.7171,4.86082],[16.69631,-21.83364]],[[277.5,-79],[-27.07578,10.9674],[13.76475,-5.57559]],[[321.5,-86],[-14.73643,1.84205],[9.28486,-1.16061]],[[349.5,-88],[-9.29988,-1.03332],[18.3122,2.03469]],[[388.5,-61],[-5.407,-19.52527],[4.11625,14.86423]],[[393.5,-15],[0.57085,-15.41308],[-2.26442,61.13942]],[[339.5,150],[31.61969,-54.06967],[-14.71672,25.16559]],[[284.5,218],[20.03708,-21.17556],[-9.48501,10.02393]],[[251.5,243],[11.55032,-7.55213],[-5.86543,3.83509]],[[232.5,252],[6.41447,-2.82236],[-1.92965,0.84905]],[[226.5,254],[1.75412,-1.16941],[-0.5547,0.3698]],[[226.5,256],[-0.5547,-0.3698],[1.75412,1.16941]],[[232.5,258],[-2.09991,-0.18666],[12.96596,1.15253]],[[271.5,260],[-13.0165,0.1228],[22.35472,-0.21089]],[[338.5,257],[-22.1753,2.83444],[22.30801,-2.8514]],[[404.5,243],[-22,4.66667],[0,0]]],"strokeColor":[0.26667,0.26667,0.26667],"strokeWidth":"4"}]' ] }, { boardId: '147114678452', viewPosition: '{x: 0, y: 0}', _id: ObjectId("57b575198bdf3a61866a7040"), undoedItems: [], boardItems: [ '["Path",{"applyMatrix":true,"data":{"id":"14770627"},"segments":[[[-49.5,-81],[0,0],[15.4526,-13.24509]],[[-2.5,-110],[-22.4746,6.1096],[49.33777,-13.41221]],[[152.5,-119],[-50.22634,-4.9403],[12.71449,1.25061]],[[189.5,-109],[-11.84984,-4.77531],[17.49673,7.05092]],[[235.5,-71],[-7.53074,-19.20339],[3.68318,9.39212]],[[239.5,-41],[-0.17097,-10.08705],[0.40843,24.09757]],[[220.5,19],[14.94704,-19.92938],[-14.14093,18.85457]],[[169.5,48],[21.04784,-8.41913],[-3.04814,1.21926]],[[160.5,52],[2.73158,-1.82105],[-0.5547,0.3698]],[[160.5,54],[-0.38749,-0.54249],[1.36999,1.91799]],[[165.5,59],[-1.84801,-1.46301],[6.16805,4.88304]],[[184.5,73],[-6.29356,-4.72017],[20.61331,15.45998]],[[224.5,111],[-15.83988,-21.59983],[5.30765,7.23771]],[[238.5,134],[-3.05281,-8.44013],[3.31953,9.17753]],[[236.5,174],[7.66365,-9.57957],[-13.60069,17.00086]],[[177.5,197],[19.18154,-1.85628],[-37.42249,3.62153]],[[70.5,185],[35.74209,12.05257],[-11.44524,-3.85944]],[[38.5,168],[10.09402,6.63322],[-1.86869,-1.228]],[[35.5,162],[1,2],[0,0]]],"strokeColor":[0.26667,0.26667,0.26667],"strokeWidth":"4"}]', '["Path",{"applyMatrix":true,"data":{"id":"14768806"},"segments":[[[404.5,-30],[0,0],[12.66667,-6]],[[442.5,-48],[-13.05438,5.10171],[16.00336,-6.25418]],[[491.5,-64],[-16.63215,4.31204],[17.12847,-4.44071]],[[554.5,-67],[-17.69749,-4.76471],[15.18153,4.08734]],[[589.5,-35],[-5.61886,-14.54293],[2.90563,7.52046]],[[592.5,-11],[0.14928,-8.06088],[-0.18679,10.08677]],[[588.5,19],[4.20342,-9.1711],[-19.59843,42.76021]],[[497.5,103],[41.48087,-22.51819],[-13.34134,7.24244]],[[454.5,118],[14.665,-3.92203],[-14.08429,3.76673]],[[411.5,126],[14.28484,-2.91527],[-2.06561,0.42155]],[[405.5,128],[0,-2.10819],[0,2.02759]],[[411.5,129],[-1.96405,-0.5036],[11.04447,2.83191]],[[444.5,138],[-10.61383,-4.16492],[15.82217,6.2087]],[[490.5,160],[-15.47325,-7.0333],[35.02983,15.92265]],[[580.5,209],[-27.86826,-26.8361],[0.36575,0.3522]],[[597.5,251],[10.07089,-10.84557],[-11.93688,12.8551]],[[537.5,266],[14.4328,0.82473],[-50.8492,-2.90567]],[[382.5,234],[47.44026,17.44127],[-18.331,-6.73934]],[[330.5,207],[16.37531,10.64395],[-4.03074,-2.61998]],[[322.5,195],[-0.39923,4.7908],[0.73082,-8.76985]],[[333.5,171],[-3.66667,8],[0,0]]],"strokeColor":[0.26667,0.26667,0.26667],"strokeWidth":"4"}]' ] }, { boardId: '147472099636', viewPosition: '{x: 0, y: 0}', _id: ObjectId("57b57583fdf7b1a30017f27c"), undoedItems: [], boardItems: [] }, { boardId: '147647695276', viewPosition: '{x: 0, y: 0}', _id: ObjectId("57b57583fdf7b1a30017f27e"), undoedItems: [], boardItems: [ '["Path",{"applyMatrix":true,"data":{"id":"14742288"},"segments":[[[260.5,-96],[0,0],[-7.05878,4.70585]],[[244.5,-81],[8.54564,-10.31371],[-12.43955,15.01326]],[[219.5,-33],[2.90916,-20.00046],[-1.39225,9.57169]],[[220.5,-4],[-1.62302,-9.53527],[2.52004,14.80525]],[[245.5,28],[-14.53916,-5.81566],[10.07634,4.03053]],[[277.5,34],[-10.8423,-0.4714],[12.36213,0.53748]],[[314.5,31],[-11.42198,4.75916],[41.46172,-17.27572]],[[407.5,-64],[-20.09147,38.03029],[10.96438,-20.754]],[[401.5,-126],[25.99163,13.2021],[-14.49254,-7.36129]],[[354.5,-139],[16.06991,2.44542],[-14.83294,-2.25719]],[[309.5,-140],[14.89683,-1.78762],[-10.46584,1.2559]],[[279.5,-130],[9.16729,-5.20306],[-43.31633,24.58494]],[[186.5,-31],[24.5229,-45.49643],[-9.79735,18.17666]],[[161.5,80],[-13.50077,-31.50181],[3.81693,8.90617]],[[183.5,99],[-9.23809,-2.92345],[18.21468,5.76414]],[[240.5,105],[-19.06562,1.22565],[28.05693,-1.80366]],[[323.5,90],[-26.23666,10.10356],[74.28502,-28.60665]],[[493.5,-70],[-38.65182,66.99649],[24.4478,-42.37619]],[[490.5,-213],[54.06671,34.83144],[-21.1374,-13.61736]],[[417.5,-232],[24.96773,2.97235],[-60.79689,-7.23772]],[[247.5,-197],[49.31791,-44.49736],[-25.26307,22.79375]],[[189.5,-113],[15.88722,-30.08943],[-6.50147,12.3134]],[[181.5,-72],[-0.96465,-13.89094],[0.77627,11.17824]],[[194.5,-41],[-7.99694,-7.84885],[31.10838,30.5323]],[[329.5,-8],[-32.74362,0.18191],[28.94394,-0.1608]],[[415.5,-20],[-28.66667,4],[0,0]]],"strokeColor":[0.26667,0.26667,0.26667],"strokeWidth":"4"}]' ] } ] } })
有更有效的方法吗?