Fastest way to update multiple records in Mongodb

307 Views Asked by At

I am trying to update (upsert) around 5k documents into the Mongodb. I am facing issue that even for 5k it is becoming very slower. I am not sure if it is because i have free Mongo Atlas account. It is taking approx 10min to update all the entries. Below is my code:

var bulk = database.collection("job_applications").initializeUnorderedBulkOp();
for (const application of applications) {
    bulk.find({application_id: application['application_id']}).upsert().replaceOne(application);
}
await bulk.execute();

I dont think it should take that much time for the 5k entries. Also whatever i researched, i found that initializeUnorderedBulkOp is the fastest way to update multiple entries still its too slow for me.

I am not sure what is the reason here. Is it my free account or it is the code?

Note: The document has around 100 properties. Though i am using find first then calling the replace so i don't think it should matter much

1

There are 1 best solutions below

0
ARITRA GHOSH On

You can try this, this is supposed to be faster, also read the complete docs here: https://www.mongodb.com/docs/rapid/reference/method/db.collection.bulkWrite/

db.collection.bulkWrite(
   [ <operation 1>, <operation 2>, ... ],
   {
      writeConcern : <document>,
      ordered : <boolean>
   }
)