我有这样的数据:
{
"_id": ObjectId("4d525ab2924f0000000022ad"),
"array": [
{ id: 1, other: 23 },
{ id: 2, other: 21 },
{ id: 0, other: 235 },
{ id: 3, other: 765 }
],
"zeroes": []
}
我想从一个数组中$pull出一个元素,并将它$push到同一文档中的第二个数组中,结果应该像这样:
{
"_id": ObjectId("id"),
"array": [
{ id: 1, other: 23 },
{ id: 2, other: 21 },
{ id: 3, other: 765 }
],
"zeroes": [
{ id: 0, other: 235 }
]
}
我意识到可以通过查找然后更新的方式来完成这个任务,即
db.foo.findOne({"_id": param._id})
.then((doc)=>{
db.foo.update(
{
"_id": param._id
},
{
"$pull": {"array": {id: 0}},
"$push": {"zeroes": {doc.array[2]} }
}
)
})
我在想是否有一个原子函数可以实现这个。 类似于,
db.foo.update({"_id": param._id}, {"$move": [{"array": {id: 0}}, {"zeroes": 1}]}
我发现了这篇慷慨地提供了我所使用数据的帖子,但问题在4年后仍未解决。过去4年中是否已经找到了解决方案?