假设我有以下集合:
{
"_id":ObjectId("562e7c594c12942f08fe4192"),
"first":[
{
"shape":"square",
"color":"blue"
},
{
"shape":"circle",
"color":"red"
}
],
"second": []
}
我想要做的是,首先在 first
数组中找到一个特定的对象,并将其移动到 second
字段
db.collection.findOneAndUpdate({_id: ObjectId("562e7c594c12942f08fe4192")}, {$pull: {first: {color: "red"}}, $push: {// that specific object to second array}})
这无法通过一次操作完成。不过,您也可以这样做:
MyModel.findOne({ _id: ObjectId("562e7c594c12942f08fe4192"), "first.color": "red" }, 'first.$', function (err, doc) {
MyModel.findByIdAndUpdate(ObjectId("562e7c594c12942f08fe4192"), {
$pull: { first: { color: "red" } },
$push: { second: doc.first[0] }
}, function (err, docs) { })
});
我是一名优秀的程序员,十分优秀!