0

I am implementing single page application and using Angular JS, MongoDB Database. So I am calling aggregation two ways:

  1. With Arguments.
  2. Without Arguments.

When I was using 3.4 version of mongoDb. Then both way was working. But I upgraded mongoDb version from 3.4 to 3.6 then without arguments it is not working. Only with argument aggregation is calling.

when I implemented aggregation without arguments.
Then It is giving me Below error message.

The full response is { 'ok' : 0.0, 'errmsg' : 'The 'cursor' option is required, except for aggregate with the explain argument', 'code' : 9, 'codeName' : 'FailedToParse'

I checked some link in stackoverflow;
Spring data mongodb - The 'cursor' option is required
The 'cursor' option is required error coming from every aggregate

GIT URL
https://github.com/metabase/metabase/issues/6599
https://github.com/Automattic/mongoose/issues/4101

But I am not getting solution. Is it defect with updated version? Or we have to change some code? Thanks In Advance

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Varun Sharma
  • 4,632
  • 13
  • 49
  • 103
  • Wow lots of formatting here. **Upgrade Mongoose**. This is not a MongoDB issue it's a "mongoose" issue. Mongoose versions prior to 5.x call a "legacy" mode of aggregate to return as a single BSON document, which is not compatible with the "default" action in MongoDB 3.6, which now expects a cursor. The code is fixed in recent mongoose releases. – Neil Lunn May 07 '18 at 08:49
  • @Neil Lunn. But recent release is itself 3.6. – Varun Sharma May 07 '18 at 11:11
  • Huh? MongDB is 3.6 [**Mongoose is version 5.x**.](http://mongoosejs.com/) It's the **Mongoose** library that is the issue here. Even the [npm link](https://www.npmjs.com/package/mongoose) just to make it sink in. version 5.0.17 at time of writing. This is the only version of "mongoose" you should be using to talk to a MongoDB 3.6 server. – Neil Lunn May 07 '18 at 11:13

1 Answers1

1

I ran into this issue because I'm using keystone cms, and hence am forced to use their somewhat outdated dependencies, which in this case includes an older version of mongoose.

I was able to avoid this error you're getting and gain aggregate functionality with the following syntax (just replace the keystone.list code with your mongoose model):

keystone.list('Blurt').model.aggregate([
    {
        $lookup:{
            from: "users",       
            localField: "author.id",   
            foreignField: "_id", 
            as: "userImg"         
        }
    }
    ,
    {
        $unwind: '$userImg'
    },
    {
        $match: { 'author.id': {$ne: req.user._id}}
    },
    {
        $sample: {'size': 3}
    },
    {
        $project: {
            _id: '$userImg._id',
            name: '$userImg.name',
            smImg: '$userImg.smImg',
            text: 1,
            vote: 1,
            blurtDate: 1,
            blurtImg: 1
        }
    }
    ]).sort({ blurtDate: -1 })
    .cursor().exec()
    .toArray(function(err, data) {
        console.log(data);
        res.json(data);
    });

.toArray() was necessary for some reason; using a callback within the .exec() method returns an empty array no matter what never could figure out why. Using mongodb 4.0.4 and mongoose 4.7.8

silencedogood
  • 3,209
  • 1
  • 11
  • 36