I am forming the query to be executed within aggregate() as follows:
query.$project = {};
query.$project.created_at = '$created_at';
query.$project.month = {};
query.$project.month.$month = currentMonth;
query.$match = {};
query.$match.month = currentMonth.getMonth() + 1;
query.$limit = 5;
query.$sort = {};
query.$sort.created_at = -1;
query.callback = function(err, result){
// do something
};
console.dir(query);
But when i execute the following:
DataTable.aggregate(query);
I get this:
Error: Arguments must be aggregate pipeline operators
at Aggregate.append (C:\myproject\node_modules\mongoose\lib\aggregate.js:87:11)
at new Aggregate (C:\myproject\node_modules\mongoose\lib\aggregate.js:47:17)
at Function.aggregate (C:\myproject\node_modules\mongoose\lib\model.js:1889:17)
at C:\myproject\app\routes.js:179:23
at C:\myproject\node_modules\async\lib\async.js:570:21
at C:\myproject\node_modules\async\lib\async.js:249:17
at C:\myproject\node_modules\async\lib\async.js:125:13
I have two question now:
I modified the above code as follows:
query.$project = {};
query.$project.created_at = '$created_at';
query.$project.month = {};
query.$project.month.$month = currentMonth;
query.$match = {};
query.$match.month = currentMonth.getMonth() + 1;
query.$limit = 5;
query.$sort = {};
query.$sort.created_at = -1;
But when i execute the following:
DataTable.aggregate(query, function(err, result){
// do something
});
Quoting Mongoose's documentation for aggregate
,
Parameters:
[...] <Object, Array>
aggregation pipeline operator(s) or operator array
[callback] <Function>
It is expecting you to pass
an array of aggregation pipeline operators
DataTable.aggregate([{
$project: {
created_at: '$created_at',
month: {
$month: currentMonth
}
}
}, {
$match: {
month: currentMonth.getMonth() + 1
}
}, {
$limit: 5
}, {
$sort: {
created_at: -1
}
}], function(err, result) {
});
or multiple aggregation pipeline operators passed as individual arguments
DataTable.aggregate({
$project: {
created_at: '$created_at',
month: {
$month: currentMonth
}
}
}, {
$match: {
month: currentMonth.getMonth() + 1
}
}, {
$limit: 5
}, {
$sort: {
created_at: -1
}
}, function(err, result) {
});
I would prefer the pipeline builder way,
DataTable.aggregate().project({
created_at: '$created_at',
month: {
$month: currentMonth
}
}).match({
month: currentMonth.getMonth() + 1
}).limit(5).sort({
created_at: -1
}).exec(function(err, result) {
});
Edit: If you prefer preparing individual items separately, then you can use an array object, like this
var query = [];
query.push({
$project: {
created_at: '$created_at',
month: {
$month: currentMonth
}
}
});
query.push({
$match: {
month: currentMonth.getMonth() + 1
}
});
query.push({
$limit: 5
});
query.push({
$sort: {
created_at: -1
}
});
DataTable.aggregate(query, function(err, result) {
});
Or using the pipeline builder,
var aggregator = DataTable.aggregate();
...
aggregator = aggregator.project({...});
...
aggregator = aggregator.match({...});
...
aggregator = aggregator.limit(...);
...
aggregator = aggregator.sort(...);
...
aggregator.exec(function(err, result) {
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With