Search code examples
node.jsmongodbmongodb-queryaggregation-frameworkloopbackjs

Aggregation issue with Mongo 3.6


I was using the aggregate function without any problem connecting a 3.4 mongodb.

When I change to a 3.6 db,

I've got the message: The 'cursor' option is required, except for aggregate with the explain argument.

Sorry if it's already posted. I am unable to find any solutions


Solution

  • In mongo 3.6 there has been changes while using aggregate you have to use cursor, unless you include the explain option, you must specify the cursor option. I faced same error as you were facing. Now you have to do it like

    this.aggregate( [
            { $unwind : "$tags" },
            {$group: {_id: '$tags', count: { $sum: 1} }},
            {$sort: { count: 1 }}
                ] ).cursor({}).exec();
    

    Now you can use cursor method cursor.toArray() to return an array that contains all the documents from a cursor. Cursors returned from aggregation only supports cursor methods that operate on evaluated cursors like cursor.toArray(), to know about more cursor methods you can click here and get further.