mongo批量使用ObjectId替换新增字段的值遇到的问题

数据库数据如下:

> 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
... functionitem{ ... 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
... functionitem{ ... 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" }

结果与预期不否,更新出错,不太明白为什么?

问题已解决,是因为对update执行没有理解到位,function里面的Update语句的执行和item没有关系,开始我以为foreach循环,item对应的就是循环项的记录,故更新的应该是item对应的那条记录,实际上执行是,update直接找满足条件的第一条记录,并不是针对当前item对应的记录做update,后来改成如下:

db.food.find{"keycode":{$exists:false}}.forEach functionitem{ item.keycode=item._id.valueOf; db.food.saveitem; }

实现了我的需求。

发表评论

电子邮件地址不会被公开。 必填项已用*标注