Using Cursor() in Mongo Aggregates with Mongoose

Viewed 1862

I recently updated mongo on my dev environment to take advantage of new functionality (Mongod 1.4 --> 4.0). Now all of my existing aggregate queries are returning an error:


{ [MongoError: The 'cursor' option is required, except for aggregate with the explain argument] name: 'MongoError', ok: 0, errmsg: 'The \'cursor\' option is required, except for aggregate with the explain argument', code: 9, codeName: 'FailedToParse' }


I know I should be able to add a 'cursor()' element to the pipeline definition (my expected return data is small), but I can't seem to get the syntax right, or find a good example online using Node/Mongoose syntax. My best guess is to add the cursor as follows:

=====================================================

ParticipantActivityLog.aggregate([
    {   $match: {
            "$and": [
                {logDate :{$gte : new Date(postDataObj.strtDt),$lt : new Date(postDataObj.endDt)}},
                {invitationId : {$in: postDataObj.invIds}}
            ]
        }
    },
    { "$sort": {logDate: 1 }},
     { $group: {
        _id: { 
            "invitationId":'$invitationId',
            "milestoneId":'$milestoneId',
            "activityId":  '$activityId'
        },
        invitationId : { $first : '$invitationId'},
        milestoneId : { $first : '$milestoneId'},
        activityId : { $first : '$activityId'},
        activityName : { $first : '$activityName'},
        logDate: {$first:'$logDate'},
        frequency1 : {$first: '$frequency1'},
        count: { $sum: 1 }
    }}         ],

{$cursor:{}}

).exec(function(err, result){
    if(err){
        console.log(err);
        return;
    }
    return res.jsonp(result);  });

=====================================================

This throws the following error...

Error: Arguments must be aggregate pipeline operators

...so I'm assuming I have the syntax of the added cursor element wrong somehow?

1 Answers

You can remove it from the array and call it outside (right before exec) Here is the updated code:

    ParticipantActivityLog.aggregate([
        {   $match: {
                "$and": [
                    {logDate :{$gte : new Date(postDataObj.strtDt),$lt : new Date(postDataObj.endDt)}},
                    {invitationId : {$in: postDataObj.invIds}}
                ]
            }
        },
        { "$sort": {logDate: 1 }},
         { $group: {
            _id: { 
                "invitationId":'$invitationId',
                "milestoneId":'$milestoneId',
                "activityId":  '$activityId'
            },
            invitationId : { $first : '$invitationId'},
            milestoneId : { $first : '$milestoneId'},
            activityId : { $first : '$activityId'},
            activityName : { $first : '$activityName'},
            logDate: {$first:'$logDate'},
            frequency1 : {$first: '$frequency1'},
            count: { $sum: 1 }
        }}         ]

    )
.cursor({})
.exec(function(err, result){
        if(err){
            console.log(err);
            return;
        }
        return res.jsonp(result);  });

You specify the cursor options like this (if you want):

.cursor({ batchSize: 2500, async: true })
Related