Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose 5.x disallows passing a spread of operators

Aggregate query returns an error.

'Mongoose 5.x disallows passing a spread of operators to Model.aggregate(). Instead of Model.aggregate({ $match }, { $skip }), do Model.aggregate([{ $match }, { $skip }])',

I am using mongoose and MongoDb version like

"mongoose": "^5.5.4", mongod version: 3.6.12

Please suggest me I will be very Thankful to you.

like image 846
Rahul Saini Avatar asked May 13 '19 13:05

Rahul Saini


People also ask

Can I use aggregate in mongoose?

Mongoose middleware also supports pre('aggregate') and post('aggregate') hooks. You can use aggregation middleware to transform the aggregation pipeline.

What does Mongoose aggregate return?

Returns: A cursor to the documents produced by the final stage of the aggregation pipeline operation, or if you include the explain option, the document that provides details on the processing of the aggregation operation. If the pipeline includes the $out operator, aggregate() returns an empty cursor.

What is pipeline in mongoose?

An aggregation pipeline consists of one or more stages that process documents: Each stage performs an operation on the input documents. For example, a stage can filter documents, group documents, and calculate values. The documents that are output from a stage are passed to the next stage.

What is aggregation in node JS?

What is the Aggregation Framework? The aggregation framework allows you to analyze your data in real time. Using the framework, you can create an aggregation pipeline that consists of one or more stages. Each stage transforms the documents and passes the output to the next stage.


1 Answers

Before

Model.aggregate({
  $group: {
    _id: "$id",
    count: { $sum: "$like.count" }
  }
})

After

Model.aggregate([{
  $group: {
    _id: "$id",
    count: { $sum: "$like.count" }
  }
}])

Explanation: Before Model.aggregate() used to take different json objects, now they have changed the API to take array.

Just wrap your JSON objects with a [] it will work.

like image 183
Gokul Kulkarni Avatar answered Sep 30 '22 19:09

Gokul Kulkarni