Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose bulk insert or update documents

I am working on a node.js app, and I've been searching for a way around using the Model.save() function because I will want to save many documents at the same time, so it would be a waste of network and processing doing it one by one.

I found a way to bulk insert. However, my model has two properties that makes them unique, an ID and a HASH (I am getting this info from an API, so I believe I need these two informations to make a document unique), so, I wanted that if I get an already existing object it would be updated instead of inserted into the schema.

Is there any way to do that? I was reading something about making concurrent calls to save the objects, using Q, however I still think this would generate an unwanted load on the Mongo server, wouldn't it? Does Mongo or Mongoose have a method to bulk insert or update like it does with insert?

Thanks in advance

like image 526
Uriel Bertoche Avatar asked Jun 06 '26 16:06

Uriel Bertoche


1 Answers

I think you are looking for the Bulk.find(<query>).upsert().update(<update>) function.

You can use it this way:

bulk = db.yourCollection.initializeUnorderedBulkOp();
for (<your for statement>) {
    bulk.find({ID: <your id>, HASH: <your hash>}).upsert().update({<your update fields>});
}
bulk.execute(<your callback>)

For each document, it will look for a document matching the {ID: <your id>, HASH: {your hash}} criteria. Then:

  • If it finds one, it will update that document using {<your update fields>}
  • Otherwise, it will create a new document

As you need, it will not make a connection to the mongo server on each iteration of the for loop. Instead a single call will be made on the bulk.execute() line.

like image 178
bagrat Avatar answered Jun 09 '26 04:06

bagrat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!