简体   繁体   English

如何使用此条件在MongoDB中更新约20,000条记录

[英]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.

相关问题 自动建议20,000个条目 - Auto suggest with 20,000 entries 如何使用 javascript 从 Firestore 数据库中检索 20,000 个文档 - How to retrieve 20,000 documents from firestore database using javascript 使用Office.js将超过20,000个单元格写入Excel中的表失败 - Writing more than 20,000 cells to a table in Excel with Office.js fails Javascript:存储20,000个数字ID并对其进行检查的最有效方法? - Javascript: Most efficient way to store 20,000 numeric IDs and check against them? 如何在nodejs和mongodb中按条件计算记录 - How to count records by criteria in nodejs & mongodb 什么是正则表达式,先作为带逗号的数值,然后是带空格的字符串(例如 - 20,000 值)? - what is the regex expression to have `first as numeric value with comma and then the string with the space (e.g - 20,000 value)? MongoDB:如何基于ObjectID更新n条记录 - MongoDB: how to update n records based on ObjectID MongoDB:如何根据记录本身的值更新多条记录? - MongoDB: How to update multiple records based on a value in the record itself? 如何在一个命令中使用非类似更新条件更新MongoDB中的多个文档? - How do I update multiple documents in MongoDB with non similar update criteria in one command? 如何计算mongodb中的记录 - how to count records in mongodb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM