0

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

2
  • $count is available from 3.4 version. Add a project stage to add the count field with $size. Something like {$project:{ count:{$size: "$dates"}, dates:1 } } Commented Jan 19, 2018 at 4:02
  • To count the document in each range you can use count:{$sum:1} in group stage Commented Jan 19, 2018 at 4:08

2 Answers 2

0

If version is 3.2 then you can use $sum in $group pipeline to get count

instead of

{ $count: "$requestDtsCal" } 

use

{$group : {_id : null, count : {$sum : 1}}} // _id your ids
Sign up to request clarification or add additional context in comments.

1 Comment

You beat me to it. Thanks.
0

I figured out an easy way to do this. I wasn't understanding groups properly.

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 ]
                } ]
            },
            count: {$sum: 1}
        }
    }
]).pretty();

Works properly.

Ian

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.