[英]How to update ~20,000 records in MongoDB with this criteria
I have a MongoDB document that looks like the following: 我有一个MongoDB文档,如下所示:
"sport": "NFL",
"team_id": 5,
"week_num": 6,
"meta": {
.... more data ....,
"season_year": 2013
}
What I'd like to do is copy the season_year
key/val to the "top level" document, while leaving it also embedded within the meta
hash. 我想做的是将season_year
键/ val复制到“顶层”文档,同时将其也嵌入到meta
哈希中。 So it would be duplicated and the end result would look like: 因此它将被复制,最终结果将如下所示:
"sport": "NFL",
"team_id": 5,
"week_num": 6,
"season_year": 2013,
"meta": {
.... more data ....,
"season_year": 2013
}
Is there a trivial way to update all of the documents in my collection with the logic described above? 有没有一种简单的方法可以使用上述逻辑更新我的收藏集中的所有文档? I am using MongoDB shell version: 2.4.3
我正在使用MongoDB shell version: 2.4.3
The problem is that during the update, you can not refer the document you are updating. 问题在于,在更新过程中,您无法引用要更新的文档。 So you can not achieve what you want in one query. 因此,您无法在一个查询中实现所需的功能。
You need to iterate through all the documents and save them one by one: 您需要遍历所有文档,并一一保存:
db.yourCollection.find({}).forEach(function(doc) {
doc.season_year = doc.meta.season_year;
db.yourCollection.save(doc);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.