How can i update all object in an array without id at MongoDB

醉酒当歌 提交于 2019-12-12 04:07:54

问题


I call the Elements with find() method and after than i want to update all. For example:

db.collection.find().limit(10).update({$set: {'column' : 'value'}}); 

how can i fix this?


回答1:


If you want to apply update to every document in collection, use {multi:true} option

db.collection.update({},{$set: {'column' : 'value'}},{multi:true}); 

For more detail, see collection.update

However, if you want to update selected number of documents, you'll be taking longer route.

db.collection.find().limit(10).forEach(function(o){
    o.column = some_value; // replace some_value with real one.
    db.collection.update({_id:o._id},o);
});



回答2:


By default it updates only the first 1 document it found. You need to add multi = true as an option to update() to update all. Unfortunately, update() doesn't have limit option so you can limit it to 10.

You might have to do find() with limit first and then update each document separately like mentioned in this post:

How to limit number of updating documents in mongodb



来源:https://stackoverflow.com/questions/36320841/how-can-i-update-all-object-in-an-array-without-id-at-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!