These are chat archives for Automattic/mongoose

14th
Jul 2018
Irfan Raza
@irfaan008
Jul 14 2018 08:55

Hi, I am using Mongoose 5.1.2 and when running aggregation pipeline I get this error :
{ "message": "Mongoose 5.x disallows passing a spread of operators to Model.aggregate(). Instead of Model.aggregate({ $match }, { $skip }), do Model.aggregate([{ $match }, { $skip }])", "name": "MongooseError" }

My pipeline is already wrapped as an array. Even when I print the raw query using debug as true, the generated query is executable in mongo shell and wrapped already as array.

var query = TestSummary.aggregate([ { $group: { _id: "$testId", count: { $sum: 1 } } }, { $limit: 10 }, { $sort: { count: -1 } } ]); query.exec(function (err, data) { }

Irfan Raza
@irfaan008
Jul 14 2018 09:03
checked with latest version 5.2.3 as well. Same error. Please assist whats wrong with my query.
Unfortunately its working with all other models the same way but not with this model only
Irfan Raza
@irfaan008
Jul 14 2018 09:33
Update : This problem occurs with group pipeline only. If I use match or any other then it works normal

Update 2 :

Very strange behaviour I found that if group is first pipeline stage then it doesn't work

Below code works
var query = TestSummary.aggregate([ { $match: { isActive: true } }, { $group: { _id: "$testId", count: { $sum: 1 } } } ]);

but this doesn't work

var query = TestSummary.aggregate([ { $group: { _id: "$testId", count: { $sum: 1 } } } ]);

Hope this much information would help you to figure out the root cause
Kev
@lineus
Jul 14 2018 10:40
@irfaan008 that's a weird one. This simple example works for me on 5.2.3
do you have any aggregate pre hooks on that schema?
can you share your schema and model declaration?
Irfan Raza
@irfaan008
Jul 14 2018 11:26
No hooks are there.
Let me check the link you shared. I will share schema then
Irfan Raza
@irfaan008
Jul 14 2018 11:39

Thanks I figured it out. There was another query which was running after this and that was not wrapped as array, which produces this error.

Thanks @lineus for your time.

Kev
@lineus
Jul 14 2018 11:39
anytime @irfaan008 :)
Irfan Raza
@irfaan008
Jul 14 2018 11:42
By the way when mongoose support for Mongodb 4.0 is planned to launch?
Kev
@lineus
Jul 14 2018 11:42
it's available in 5.2 :)
Irfan Raza
@irfaan008
Jul 14 2018 11:42
Oh cool
Eldar
@Eldar-X
Jul 14 2018 12:44
Hi! I try to make sub lookup. I have hotels collection then i make join contracts to hotels and after that i need to go contracts.rooms.type and populate this field with single room information. For now i can join to root of document but not into contracts.rooms array and result must be object but it's array :( please help me!
 {
      $match: {
        $or: [
          { name: { $regex: nameOrLocation, $options: 'i' } },
          { zone: { $regex: nameOrLocation, $options: 'i' } },
          { subzone: { $regex: nameOrLocation, $options: 'i' } },
          { city: { $regex: nameOrLocation, $options: 'i' } },
          { country: { $regex: nameOrLocation, $options: 'i' } }
        ]
      }
    },
    {
      $lookup: {
        from: Model.Contract.collection.name,
        let: { hotelId: '$_id' },
        pipeline: [
          {
            $match: {
              $expr: {
                $and: [
                  { $eq: ['$$hotelId', '$hotel'] },
                  { $eq: [true, '$isActive'] },
                  { $in: [market, '$countries'] }
                ]
              }
            }
          },
          {
            $lookup: {
              from: Model.Room.collection.name,
              localField: 'rooms.type',
              foreignField: '_id',
              as: 'type'
            }
          }
        ],
        as: 'contracts'
      }
    },
    {
      $match: {
        'contracts.rooms.maxAdults': { $gte: Number(numOfAdults) || 1 },
        'contracts.rooms.minAdults': { $lte: Number(numOfAdults) || 1 },
        'contracts.rooms.maxChildren': { $gte: Number(numOfChildren) || 0 },
        'contracts.rooms.minChildren': { $lte: Number(numOfChildren) || 0 },
        'contracts.rooms.periods.startDate': { $lte: new Date(startDate) },
        'contracts.rooms.periods.endDate': { $gte: new Date(endDate) }
      }
    }
Eldar
@Eldar-X
Jul 14 2018 12:53
It can be fine if in my room.type lookup i can set cursor at current iterated room
Kev
@lineus
Jul 14 2018 17:36
@Eldar-X can you share the 3 relevant Schema? I'll play around with it and see if I can get to work.