mongoose: Cursor operator in Aggregate function not working..

Hi,

I have the latest mongoose 3.8.16 with mongodb 2.6.4

I am trying to do cursor operation on an aggregate pipeline.

My function looks like,

var where = {  "storeId": storeId };
MongoBasket.aggregate()
                            .match(where)
                            .group({
                                "basketProducts": {
                                    "$push": "$basketProduct"
                                },
                                "basketIds": {
                                    "$push": "$basketId"
                                },
                                "storeId": {
                                    "$addToSet": "$storeId"
                                },
                                "consumerIds": {
                                    "$push": "$consumerId"
                                },
                                "basketTransactionCardTotal": {
                                    "$push": "$basketTransactionCard"
                                },
                                "basketTransactionCashTotal": {
                                    "$push": "$basketTransactionCash"
                                },
                                "totalTip": {
                                    "$push": "$tip"
                                },
                                "_id": {
                                    "year": {
                                        "$year": "$completed"
                                    },
                                    "month": {
                                        "$month": "$completed"
                                    },
                                    "day": {
                                        "$dayOfMonth": "$completed"
                                    },
                                    "hour": {
                                        "$hour": "$completed"
                                    }
                                }
                            })
                            .sort({_id: 1})
                            .cursor({
                                batchSize: 200000
                            })
                            .exec(function(err, result) {
                                 console.log(err, result);
                            }, req.messageInstance);

I printed the raw mongodb query that is built by mongoose (using mongoose.set(‘debug’, true)) which looks like

baskets.aggregate([
    {
        '$match': {
            storeId: 11
        }
    },
    {
        '$group': {
            _id: {
                hour: {
                    '$hour': '$completed'
                },
                day: {
                    '$dayOfMonth': '$completed'
                },
                month: {
                    '$month': '$completed'
                },
                year: {
                    '$year': '$completed'
                }
            },
            totalTip: {
                '$push': '$tip'
            },
            basketTransactionCashTotal: {
                '$push': '$basketTransactionCash'
            },
            basketTransactionCardTotal: {
                '$push': '$basketTransactionCard'
            },
            consumerIds: {
                '$push': '$consumerId'
            },
            storeId: {
                '$addToSet': '$storeId'
            },
            basketIds: {
                '$push': '$basketId'
            },
            basketProducts: {
                '$push': '$basketProduct'
            }
        }
    },
    {
        '$sort': {
            _id: 1
        }
    }
]){
    cursor: {
        batchSize: 200000
    }
}

Please note that cursor operator is outside the aggregate () enclosure

]){
    cursor: {
        batchSize: 200000
    }
}

Shouldn’t it be like

],
{
    cursor: {
        batchSize: 200000
    }
})

My hand written raw mongodb query for mongo shell works really well and here is how it looks


db.baskets.aggregate([
    {
        "$match": {
            storeId: 11
        }

    },
    {
        "$group": {
            "_id": {
                "year": {
                    "$year": "$completed"
                },
                "month": {
                    "$month": "$completed"
                },
                "day": {
                    "$dayOfMonth": "$completed"
                },
                "hour": {
                    "$hour": "$completed"
                }
            },
            "totalTip": {
                "$push": "$tip"
            },
            "basketTransactionCashTotal": {
                "$push": "$basketTransactionCash"
            },
            "basketTransactionCardTotal": {
                "$push": "$basketTransactionCard"
            },
            "consumerIds": {
                "$push": "$consumerId"
            },
            "storeId": {
                "$addToSet": "$storeId"bas
            },
            "basketIds": {
                "$push": "$basketId"
            },
            "basketProducts": {
                "$push": "$basketProduct"
            }
        }
    },
    {
        "$sort": {
            _id: 1
        }
    }
],
{
    cursor: {
        batchSize: 2000000
    }
})

Is it a bug or am I missing something?

If someone could shred some light on it would be much appreciated.

Many thank,

Karthik

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 15

Commits related to this issue

Most upvoted comments

@Ra1da35ma well that’s embarrassing, looks like we closed out #2955 by mistake. Thanks for pointing that out, re-opened and will be fixed 👍

Yep those docs are incorrect, thanks for pointing that out

This works:

Model
.aggregate(pipeline)
.exec(function () {
  console.log(arguments);
})

And this doesn’t: (never calls back)

Model
.aggregate(pipeline)
.cursor({ batchSize: 1000 })
.exec(function () {
  console.log(arguments);
})

Also, the following doesn’t return a promise:

Model
.aggregate(pipeline)
.cursor({ batchSize: 1000 })
.exec()

Am I missing something?