Uriel Bertoche
Uriel Bertoche

Reputation: 883

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

Upvotes: 3

Views: 5431

Answers (1)

bagrat
bagrat

Reputation: 7418

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.

Upvotes: 4

Related Questions