npm i mongo-aggregator
Usage:
var expander = require('mongo-aggregator');
There are three methods:
-
expander.expandOne(childPath, projection, [childCollectionName])
for expand simple (not array) field.
-
expander.expandMany(childPath, projection, [childCollectionName])
for expand array field.
-
expander.expandManyWithChildProjection =
function (childPathName, projection, childProjection, childCollectionName)
for expand array field with projection for child documents.
A result of functions will be aggregation obj.
Next:
// mongoose
MyModel.aggregate(aggregationObj, function(err, myModels) {
...
});
// or mongo driver
db.collection('myCollection').aggregate(aggregationObj, function(err, myDocs) {
...
});
Projection example:
{
name: 1,
age: 1
}
For running tests install mocha globally before: npm i mocha -g
And: npm run test