Search code examples
mongodbmongooseaggregatemongoose-schemamongoose-populate

Aggregate not working as expected in mongoose


Here's a lookup query that i'm using

  {
          $lookup: {
            from: 'weeks',
            localField: 'weeks',
            foreignField: '_id',
            as: 'weeks'
          }
   }

Result with this query

"weeks": [
                    {
                        "_id": "619e87d7b1bd6501c7aae286",
                        "name": "week-1",
                        "description": "Commodo in o.",
                        "course": "619e87d7b1bd6501c7aae281",
                        "days": [
                            "619e87dab1bd6501c7aae2a8",
                            "619e87dab1bd6501c7aae2a9",
                            "619e87dab1bd6501c7aae2aa",
                            "619e87dab1bd6501c7aae2ab",
                            "619e87dab1bd6501c7aae2ac",
                            "619e87dab1bd6501c7aae2ad"
                        ],
                        "isCopy": false,
                        "__v": 0
                    },
                    {
                        "_id": "619e87d7b1bd6501c7aae287",
                        "name": "week-2",
                        "description": "Irure e.",
                        "course": "619e87d7b1bd6501c7aae281",
                        "days": [
                            "619e87dab1bd6501c7aae2db",
                            "619e87dab1bd6501c7aae2dc",
                            "619e87dab1bd6501c7aae2dd",
                            "619e87dab1bd6501c7aae2de",
                            "619e87dab1bd6501c7aae2df",
                            "619e87dab1bd6501c7aae2e0"
                        ],
                        "isCopy": false,
                        "__v": 0
                    },]

In the above lookup localField weeks is an array of object Id's. When i execute this code it works as expected, but i want to use the same functionality with pipeline. Here's the code i wrote

{
          $lookup: {
            from: "weeks",
            let: { wks: "$weeks" },
            pipeline: [
              {
                $match: {
                  _id: {
                    $in: ["$$wks"]
                  }
                }
              }
            ],
            as: "weeks"
          }
        }

Result with this query `weeks:[]`

When i run this, i don't get anything in output, the reason for that in my opinion is that weeks array is being interpreted as a string instead of object id.

How do i fix it now ...


Solution

  • Is this what you need ? Check my mongoplayground below, if not, make your own sample data and share a new link with me.

    db.collection.aggregate([
      {
        $lookup: {
          from: "weeks",
          let: {
            wks: "$weeks"
          },
          pipeline: [
            {
              $match: {
                $expr: {
                  $in: [
                    "$_id",
                    "$$wks"
                  ]
                }
              }
            }
          ],
          as: "weeks"
        }
      }
    ])
    

    mongoplayground