javascript - Mongoose find all referenced documents -
does mongoose provide way find referenced documents previous query' result?
for example:
product .find(query) .exec(function(err, results) { ... product .find({ '$or': [ // products _id in results.associatedproducts[] ] }) ... });
i wasn't able find way natively mongoose, see below working solution.
product .find(query) .exec(function(err, products) { ... var associatedsoftware = products.reduce(function(memo, current) { if(current.features && current.features.associatedsoftware) { return memo.concat(current.features.associatedsoftware.map(function(item) { return item._id; })); } return memo; }, []); product.find({ '$or': [ ... { '_id': { '$in': associatedsoftware } } ... ] }) .exec(function(err, associated) { ... }); });
Comments
Post a Comment