G
G
goodweather2017-07-27 19:09:17
MongoDB
goodweather, 2017-07-27 19:09:17

How to add the result of another query to the result of one query?

exports.all = function(cb) {

   db.get().collection('tasks').find({"parent_id": null}).sort({order: 1}).toArray(function(err, docs) {	
   	
   	docs.forEach( function (x) {
      x.sub = db.get().collection('tasks').find({"parent_id": x._id.toString()}).sort({order: 1}).toArray();
    });
   	cb(err, docs);
   });	
};

As a result of executing this code, not an array is placed in x.sub, but a promise.
How to change the code to get the following result
{ _id: 1
name: 'Person1',
parent_id : 0,
sub : [
{_id: 3, name: 'Person3', parent_id : 1},
{_id: 4,name: ' Person4', parent_id : 1},
]
},
{ _id: 2
name: 'Person2',
parent_id : 0,
sub: []
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question