javascript - closing mongodb connection and get correct results with multiple parallel asynchronous query -


I'm new to Mongo and node I'm currently using Mongoscin and Blubber to handle debug connections and queries I am (as told here :).

I have three collections: users, binders and cards.

Binders Collection The information about the card for each user is the properties of each document in the binders:

  User id & lt; --- which refers to the owner of the card card code; & Lt; --- This is a card calculation & lt; - This refers to the number of cards owned by the user.  

I like to store a separate card, so that when a card changes, it all varies for user binders.

I now want to retrieve an array for a given user such as:

  [{card: {card document}, count: 4} ...]  

I have the following problems:

  • DB connection should be closed after calling all ASIN DB callback
  • Card <
  • There may be a starting point for discussion:

      var getAllBinderCards = function (req, res) {var db = req.db; Var userId = req.userId; Var debaters = db.collection ('Binders'). Find ({userId: userId}). ToArrayAsync (); Promises. Then (function (binder card) (if (binder card) {var wired cards; //console.log(------- binder card: ", binder card); var card = []; I = 0; i & lt; binderCards.length; i ++) {var binderCard = binderCards [i]; promiseCards = db.collection ('card') ({code: binderCard.Code}) toArrayAsync () ; PromiseCards.then (function (.cardsDB) {if found (cardsDB) {//console.log("Cards found: ", binderCard.Code, cardsDB); for (I = 0; I & lt; cardsDB.length ; I ++) {cardsDB [i] .count = BinderCard.count;}; cards.concat (cardsDB);}});} Wired card. Then (function () {db.close (); console.log ( "Binder card:", card); res.json (leaves);});}});}  

    I am trying to figure out how to handle the correctly processed asynchronous call to send back the whole message and close the DB connection.

    I think I should try to make a promise before the loop and use it to ask questions on the promises of the card and in the end we have to chain db.close () and res.json (cards) statements.

    [edit] Perhaps the easiest solution is to use $ FILT in any one db.collection ('card') ({code: {$ in: [bindersCodeArray] }}) .. ToArrayAsync (); and avoid that loop:

      var getAllBinderCards = function (req, res) {var db = req.db; Var userId = req.userId; Var debaters = db.collection ('Binders'). Find ({userId: userId}). ToArrayAsync (); PromiseBinders.then (function (binderCards) {if (binderCards) {var binderCodes = binderCards.map (function) {element.Code return;}); .wirecardcards = db.collection ('card') appears ({ Code: {$ in: binderCodes}.}) ToArrayAsync (); promiseCards.then (function (for card) {var bindersDictionary = {}; (Var I = 0; I & lt; binderCards.length; i ++) {BindersDictionary [binderCards [I]. Codes] = Binder card [i] for count;}; (var i = 0; i & lt; cards.length; i ++) {card [i]. Count = bindersDictionary [Card [i]. Code];}; db.close (); console.log ("binder card:", card); res.json (card);});}}); }  

    Still I'm curious if this puzzle is a great way to solve with the use of promise.

I would expect that there could be constraints on the number of binary in $ IN and array that you pass Can and affect query execution. You can also try this with. For example:

  ... function (binders) {async.map (binders, cardsForBinders, function (hey, bindersWithCards) {// TODO: close connection here} function cardsForBinders (tying Caller, callback) {// 1. Find the card for the binder 2. 2. Prepare a transmitted response: Binder with card, callback (empty, binder with card);}} ...  

Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

android - how to get distance of 2 beacons and make a condtion base on that distance -

c# - WPF Expander overlay used in ItemsControl -