Execute mulitple calls to Mongo, then wait for them all to return

Execute mulitple calls to Mongo, then wait for them all to return

Problem

So after posting some other questions and doing some research it does not look like it is possible to update multiple documents with different data in the same call to mongodb.

doc1 : {
  _id: 1,
  name: 'John Smith'
}

doc2 : {
  _id: 2,
  name: 'Jane Smith'
}

So if the client passes both docs to the server adds an age field and wants a response back from the server when both have been updated I have to wait for two update callbacks to return.

Not a huge deal at all as there look to be some good async modules out there like Step and Async. However I am using Mongoose already. I see that there are promises in mongoose. Looking at the docs I don’t think this is possible but it may be that I just don’t understand.

Using mongoose can I submit N number of updates to mongo and know when all N have completed so I can response to the client. Without a counter of course (as that is what I am already doing).

Thanks!

Problem courtesy of: lostintranslation

Solution

I believe not, instead use async (or possibly step, i’m not familiar) to run multiple queries/queries multiple times.

Solution courtesy of: Ben Evans

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *