就像@ blakes-seven所说的那样,您有两种方法可以做到:
抓取,更新和推送
db.images.find({ _id: '' }, { images : 1 }).then(function(img) { var tmpImg = img.images[0]; img.images[0] = img.images[1]; img.images[1] = tmpImg; db.images.update({ _id: img._id }, { $set: { images: img.images } });})__使用 $ position* 直接更新 (如果您在客户端上有映像并且知道索引) *
db.images.update({ _id: '' }, { $push: { images: { $each: ['img2'] }, $position: 0 } } } ).then(function() { db.images.update({ _id: '' }, {$unset: {'images.2': 1}})});https://docs.mongodb.org/manual/reference/operator/update/position/
但是,我认为您应该重新设计存储图像的方式,例如使用虚拟顺序:
{ images: [ { base64: 'img1', order: 0, _id: 'img1' }, { base64: 'img2', order: 1, _id: 'img2' }, { base64: 'img3', order: 2, _id: 'img3' } ]}这样,您可以使用虚拟订单索引对图像进行排序,仅使用_id对其进行更新,或者通过更改所有img2的顺序,删除或替换图像等来更新整个集合。



