聚合在猫鼬中无法按预期工作

Aggregate not working as expected in mongoose

这是我正在使用的查找查询

  {
          $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
                    },]

在上面的查找中,localField weeks 是对象 ID 的数组。 当我执行此代码时,它按预期工作,但我想对管道使用相同的功能。 这是我写的代码

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

Result with this query `weeks:[]`

当我 运行 这样做时,我没有得到任何输出,我认为原因是周数组被解释为字符串而不是对象 ID。

我现在该如何解决...

这是您需要的吗?检查下面我的 mongoplayground,如果没有,请制作您自己的示例数据并与我分享一个新的 link。

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

mongoplayground