MongoDB:更新一个字段上的每个文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9038547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB: update every document on one field
提问by randombits
I have a collected named foo
hypothetically.
我有一个foo
假设命名的集合。
Each instance of foo
has a field called lastLookedAt which is a UNIX timestamp since epoch. I'd like to be able to go through the MongoDB client and set that timestamp for all existing documents (about 20,000 of them) to the current timestamp.
每个实例foo
都有一个名为 lastLookedAt 的字段,它是自纪元以来的 UNIX 时间戳。我希望能够通过 MongoDB 客户端并将所有现有文档(大约 20,000 个)的时间戳设置为当前时间戳。
What's the best way of handling this?
处理这个问题的最佳方法是什么?
回答by Philippe Plantier
Regardless of the version, for your example, the <update>
is:
无论版本如何,对于您的示例,<update>
都是:
{ $set: { lastLookedAt: Date.now() / 1000 } }
However, depending on your version of MongoDB, the query will look different. Regardless of version, the key is that the empty condition {}
will match any document. In the Mongo shell, or with any MongoDB client:
但是,根据您的 MongoDB 版本,查询看起来会有所不同。无论版本如何,关键是空条件{}
匹配任何文档。在 Mongo shell 或任何 MongoDB 客户端中:
db.foo.updateMany( {}, <update> )
{}
is the condition (the empty condition matches any document)
{}
是条件(空条件匹配任何文档)
db.foo.update( {}, <update>, { multi: true } )
{}
is the condition (the empty condition matches any document){multi: true}
is the "update multiple documents" option
{}
是条件(空条件匹配任何文档){multi: true}
是“更新多个文档”选项
db.foo.update( {}, <update>, false, true )
{}
is the condition (the empty condition matches any document)false
is for the "upsert" parametertrue
is for the "multi" parameter (update multiple records)
{}
是条件(空条件匹配任何文档)false
用于“ upsert”参数true
用于“multi”参数(更新多条记录)
回答by Jitendra
This code will be helpful for you
此代码将对您有所帮助
Model.update({
'type': "newuser"
}, {
$set: {
email: "[email protected]",
phoneNumber:"0123456789"
}
}, {
multi: true
},
function(err, result) {
console.log(result);
console.log(err);
})
回答by user1163459
I have been using MongoDB .NET driver for a little over a month now. If I were to do it using .NET driver, I would use Update method on the collection object. First, I will construct a query that will get me all the documents I am interested in and do an Update on the fields I want to change. Update in Mongo only affects the first document and to update all documents resulting from the query one needs to use 'Multi' update flag. Sample code follows...
我已经使用 MongoDB .NET 驱动程序一个多月了。如果我使用 .NET 驱动程序来做,我会在集合对象上使用 Update 方法。首先,我将构建一个查询,该查询将获取我感兴趣的所有文档并对我想要更改的字段进行更新。Mongo 中的更新仅影响第一个文档,要更新查询产生的所有文档,需要使用“多”更新标志。示例代码如下...
var collection = db.GetCollection("Foo");
var query = Query.GTE("No", 1); // need to construct in such a way that it will give all 20K //docs.
var update = Update.Set("timestamp", datetime.UtcNow);
collection.Update(query, update, UpdateFlags.Multi);