我如何在MongoDB上更新没有ID的数组中的所有对象 [英] How can i update all object in an array without id at MongoDB

查看:139
本文介绍了我如何在MongoDB上更新没有ID的数组中的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用find()方法调用Elements,之后我想更新所有元素.例如:

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'}}); 

我该如何解决?

推荐答案

如果要对集合中的每个文档应用更新,请使用{multi:true}选项

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);
});

这篇关于我如何在MongoDB上更新没有ID的数组中的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆