1 votes

mongoose aggregate problem

I am trying to create an aggregate using mongoose version 5.x and I get the following error when doing so

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

the code I am using to create the aggregate is as follows

  eventos.aggregate(
  { "$match": { "fecha": f } },
    {
      "$lookup":
        {
          "from": "usuarios",
          "localField": "googleId",
          "foreignField": "googleId",
          "as": "user"
        }
    },
    { "$unwind": "$user" }

    ,function (err, evento) {
    if (err){
      return handleError(err);
    } else{

      console.log(evento);
    }
  });

1voto

the solution I found was to structure it as a pipeline and it works as it should.

 eventos.aggregate().
match({ "fecha": f }).
lookup( {
  "from": "usuarios",
  "localField": "googleId",
  "foreignField": "googleId",
  "as": "user"
}).
unwind("$user").
exec(function (err, evento) {
  if (err){
    return handleError(err);
  } else{
    res.json({code:201,events:evento});
    console.log(evento);
  }
})

0voto

ElGerar Points 1830

It seems that the error tells you that you have not put the brackets ( [ ] ) in the function. I don't know if it has another error but what it asks you to do is the following:

eventos.aggregate([ //Aquí lo abro
  { "$match": { "fecha": f } },
    {
      "$lookup":
        {
          "from": "usuarios",
          "localField": "googleId",
          "foreignField": "googleId",
          "as": "user"
        }
    },
    { "$unwind": "$user" }] //Aquí cierro
    ,function (err, evento) {
        if (err){
          return handleError(err);
        } else{

          console.log(evento);
        }
    });

Greetings

0 votes

I did what you indicated but it still asked me to structure it as a pipeline, I did it this way and it worked correctly.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X