Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

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

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×