Thursday, 18 May 2017

Mongoose store query result in variables

How can store the query result from an aggregate function into a variable and use it in other functions. i have this function to count the records:

const utilitiesFunctions = {};
utilitiesFunctions.conteoBatch = (callback) => {
  workOrder.aggregate({
    $unwind: "$batches"
  }, {
    $group: {
      _id: "$batches",
      total: {
        $sum: 1
      }
    }
  }, function(err, conteos) {
    callback(conteos);
  });
}

module.exports = utilitiesFunctions;

the resutl is:

[ { _id: 591c9fc757ddf611fc7c5840, total: 2 },
  { _id: 591c9a9e5683f81b8c48e44c, total: 1 } ]

I use a LOOP to search for the _id with the lower count. how can pass the id to a variable to use it in other validation functions, something like this:

var idb = utilitiesFunctions.conteoBatch();//idb=591c9fc757ddf611fc7c5840



via joselegit

No comments:

Post a Comment