admin管理员组文章数量:1391960
I have a MongoDB document that looks like the following:
"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. 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
I have a MongoDB document that looks like the following:
"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. 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
1 Answer
Reset to default 7The 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);
});
本文标签: javascriptHow to update 20000 records in MongoDB with this criteriaStack Overflow
版权声明:本文标题:javascript - How to update ~20,000 records in MongoDB with this criteria - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744669637a2618747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论