首页  >  问答  >  正文

java - 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(
...    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" }

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

黄舟黄舟2764 天前426

全部回复(1)我来回复

  • PHP中文网

    PHP中文网2017-04-17 14:49:04

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

    db.food.find({"keycode":{$exists:false}}).forEach(
         function(item){ 
             item.keycode=item._id.valueOf(); 
             db.food.save(item); 
    })

    实现了我的需求。

    回复
    0
  • 取消回复