mongoose-aggregate-paginate-v2 icon indicating copy to clipboard operation
mongoose-aggregate-paginate-v2 copied to clipboard

$lookup operator does not work

Open ar124officialwd opened this issue 3 years ago • 3 comments

I'm unable to use $lookup operator in aggregatePaginate. The expected result property (products in query below) is always null and excluded from the results document at all. It's otherwise working fine without paginate (using aggregate method)

Following is the query, I'm running.

  query = [
    { $match: {} },

    {
      $lookup: {
        from: 'products',
        let: { attraction: '$_id' },
        // localField: '_id',
        // foreignField: 'attraction',
        as: 'products',
        pipeline: [
          {
            $match: {
              $expr: {
                $eq: ['$attraction', '$$attraction'],
              },
            },
          },

          { $project: { price: 1 } },

          { $sort: { 'price.price': 1 } },

          { $limit: 1 },
        ],
      },
    },
  ];

ar124officialwd avatar Dec 27 '21 13:12 ar124officialwd

I'm running into the same problem. Did you find a solution @ar124official2019 ?

mikelinden1 avatar Feb 01 '23 20:02 mikelinden1

Actually, I figured it out. I was passing my pipeline stages to aggregatePaginate instead of an aggregate.

mikelinden1 avatar Feb 01 '23 20:02 mikelinden1

Actually, I figured it out. I was passing my pipeline stages to aggregatePaginate instead of an aggregate.

I don't exactly remember that problem, yet I clearly remember that I opted out of it and rather wrote my custom aggregate. I would definitely reproduce it and try your solution.

Edit: infact my problem was that I wanted to aggregate with paginate and that did never work for me, so I wrote custom logic.

ar124officialwd avatar Feb 01 '23 20:02 ar124officialwd