简体   繁体   中英

mongodb update structure of database, super slow

If I need to update the structure of 70,000 documents. Looping through the cursor and updating each one is super super slow. It does about 10/s

Any tips on how to update the document structure (essentially updating some values in every document in my database)

import json
import pymongo
from bson.objectid import ObjectId

for document in applications.find({}):
    document["name"] = {"name": document["name"], "level": "999"}
    applications.update_one({}, {"$set": document})

The database is hosted on a decent enough server, and I feel like its the code which is limiting the speed. Not the server.

https://gyazo.com/f8227e0cd24c3f5c9c0cb67e52e5a9ca

Your current attempt is iterating the find() result set at js level and fire individual update back to database, which creates a lot of overhead and slow.

With MongoDB v4.2+, you can update with an aggregation pipeline.

db.collection.update({},
[
  {
    $addFields: {
      name: {
        name: "$name",
        level: "999"
      }
    }
  }
],
{
  multi: true
})

Here is the Mongo playground for your reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM