数据库数据如下:
> db.food.find()
{ "_id" : ObjectId("551e02da874e367bbf0b218e"), "fruit" : [ "apple", "orange", "pear" ] }
{ "_id" : ObjectId("551e044e874e367bbf0b218f"), "fruit" : [ "strawberry", "orange" ] }
{ "_id" : ObjectId("551e01ca874e367bbf0b218b"), "fruit" : [ "apple", "banana", "peach", "apricot" ] }
{ "_id" : ObjectId("551e0220874e367bbf0b218c"), "fruit" : [ "apple", "orange", "cherry" ] }
{ "_id" : ObjectId("551e026a874e367bbf0b218d"), "fruit" : [ "strawberry", "orange", "pear" ] }
现在需求是新增字段keycode,使其值是ObjectId的值,使用如下Update语句,可以执行批量更新:
> db.food.find().forEach(
... function(item){
... db.food.update({"keycode":{$exists:false}},{"$set":{"keycode":item._id.valueOf()}})
... }
... )
> db.food.find()
{ "_id" : ObjectId("551e02da874e367bbf0b218e"), "fruit" : [ "apple", "orange", "pear" ], "keycode" : "551e02da874e367bbf0b218e" }
{ "_id" : ObjectId("551e044e874e367bbf0b218f"), "fruit" : [ "strawberry", "orange" ], "keycode" : "551e044e874e367bbf0b218f" }
{ "_id" : ObjectId("551e01ca874e367bbf0b218b"), "fruit" : [ "apple", "banana", "peach", "apricot" ], "keycode" : "551e01ca874e367bbf0b218b" }
{ "_id" : ObjectId("551e0220874e367bbf0b218c"), "fruit" : [ "apple", "orange", "cherry" ], "keycode" : "551e0220874e367bbf0b218c" }
{ "_id" : ObjectId("551e026a874e367bbf0b218d"), "fruit" : [ "strawberry", "orange", "pear" ], "keycode" : "551e026a874e367bbf0b218d" }
现在遇到的问题是如果,我只更新符合条件的数据,却发现更新错误,如下:
> db.food.find()
{ "_id" : ObjectId("551e02da874e367bbf0b218e"), "fruit" : [ "apple", "orange", "pear" ], "keycode" : "551e02da874e367bbf0b218e" }
{ "_id" : ObjectId("551e044e874e367bbf0b218f"), "fruit" : [ "strawberry", "orange" ] }
{ "_id" : ObjectId("551e01ca874e367bbf0b218b"), "fruit" : [ "apple", "banana", "peach", "apricot" ], "keycode" : "551e01ca874e367bbf0b218b" }
{ "_id" : ObjectId("551e0220874e367bbf0b218c"), "fruit" : [ "apple", "orange", "cherry" ] }
{ "_id" : ObjectId("551e026a874e367bbf0b218d"), "fruit" : [ "strawberry", "orange", "pear" ] }
> db.food.find().forEach(
... function(item){
... db.food.update({"keycode":{$exists:false}},{"$set":{"keycode":item._id.valueOf()}})
... }
... )
> db.food.find()
{ "_id" : ObjectId("551e02da874e367bbf0b218e"), "fruit" : [ "apple", "orange", "pear" ], "keycode" : "551e02da874e367bbf0b218e" }
{ "_id" : ObjectId("551e044e874e367bbf0b218f"), "fruit" : [ "strawberry", "orange" ], "keycode" : "551e02da874e367bbf0b218e" }
{ "_id" : ObjectId("551e01ca874e367bbf0b218b"), "fruit" : [ "apple", "banana", "peach", "apricot" ], "keycode" : "551e01ca874e367bbf0b218b" }
{ "_id" : ObjectId("551e0220874e367bbf0b218c"), "fruit" : [ "apple", "orange", "cherry" ], "keycode" : "551e044e874e367bbf0b218f" }
{ "_id" : ObjectId("551e026a874e367bbf0b218d"), "fruit" : [ "strawberry", "orange", "pear" ], "keycode" : "551e01ca874e367bbf0b218b" }
结果与预期不否,更新出错,不太明白为什么?
PHP中文网2017-04-17 14:49:04
The problem has been solved because the update execution is not fully understood. The execution of the Update statement in the function has nothing to do with the item. At first, I thought that in the foreach loop, the item corresponds to the record of the loop item, so the updated item should correspond to the item. The actual execution of the record is that the update directly finds the first record that meets the conditions. It does not update the record corresponding to the current item. It was later changed to the following:
db.food.find({"keycode":{$exists:false}}).forEach(
function(item){
item.keycode=item._id.valueOf();
db.food.save(item);
})
Achieved my needs.