I'm trying to build an aggregation pipeline in Mongo to count the number of documents generated every 10 minutes in a fairly large dataset. Each document contains an ISODate in a field called requestDtsCal. I'm trying the following code (thanks to https://stackoverflow.com/users/3943271/wizard for the base code):
var baseDate = new Date(2017, 01, 11, 00, 00, 0);
var startDate = new Date(2017, 01, 11, 00, 00, 0);
var endDate = new Date(2018, 09, 20, 14, 25, 0);
var divisor = 10 * 60 * 1000; // 10 minutes in miliseconds
db.AUDIT.aggregate([
{
$match : {
requestDtsCal : {
$gte : startDate,
$lt : endDate
}
}
}, {
$group : {
_id : {
$subtract : [ "$requestDtsCal", {
$mod : [ {
$subtract : [ "$requestDtsCal", baseDate ]
}, divisor ]
} ]
},
dates : {
$push : "$requestDtsCal"
}
}
}, {
$count: "$requestDtsCal"
}
]).pretty();
If I run it without the last pipeline stage it returns an array of arrays of all the dates from each document within each range. As soon as I try and count the number of documents in each range with the last pipeline stage it fails with:
assert: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized pipeline stage name: '$count'",
"code" : 16436
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:403:5
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1212:12
@(shell):1:1
2018-01-18T19:54:40.669-0800 E QUERY [thread1] Error: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized pipeline stage name: '$count'",
"code" : 16436
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:403:5
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1212:12
@(shell):1:1
Any ideas what I'm doing wrong? This is running against Mongo 3.2.11 FWIW.
Thanks,
Ian
$countis available from 3.4 version. Add a project stage to add the count field with $size. Something like{$project:{ count:{$size: "$dates"}, dates:1 } }count:{$sum:1}in group stage