嵌套数组中的 MongoDB 查找

帕拉布

我正在尝试在 MongoDB 嵌套数组中进行查找。我的数据看起来像。

[
    {
        "_id": "621eedae92979fd8f0e9451d",
        "name": "Pallab Koley",
        "shifts": {
            "_id": "62636b9fcbda6d2b17f5cae0",
            "month": "2022-05",
            "shift": [
                {
                    "date": "2022-05-01",
                    "shiftId": "622bb0f4b88dc92e3c2cac56"
                },
                {
                    "date": "2022-05-02",
                    "shiftId": "622b55f8f59dcdd1ab9b36b1"
                },
            ]
        }
    },
    {
        "_id": "62626a7446ba9a911a623b37",
        "name": "Pinki Das",
        "shifts": {
            "_id": "62636ba4cbda6d2b17f5cae1",
            "month": "2022-05",
            "shift": [
                {
                    "date": "2022-05-01",
                    "shiftId": "622bb0f4b88dc92e3c2cac56"
                }
            ]
        }
    }
]

我正在尝试查找。

{
    "$lookup": {
        "from": "shifts",
        "localField": "shifts.shift.shiftId",
        "foreignField": "_id",
        "as": "shifts.shift.shiftId"
    }
}

并得到结果。

[
  {
    "_id": "621eedae92979fd8f0e9451d",
    "name": "Pallab Koley",
    "shifts": {
      "_id": "62636b9fcbda6d2b17f5cae0",
      "month": "2022-05",
      "shift": {
        "date": "2022-05-01",
        "shiftId": [
          {
            "_id": "622bb0f4b88dc92e3c2cac56",
            "date": "2022-05-01",
            "name": "Day"
          }
        ]
      }
    }
  },
  {
    "_id": "621eedae92979fd8f0e9451d",
    "name": "Pallab Koley",
    "shifts": {
      "_id": "62636b9fcbda6d2b17f5cae0",
      "month": "2022-05",
      "shift": {
        "date": "2022-05-02",
        "shiftId": [
          {
            "_id": "622b55f8f59dcdd1ab9b36b1",
            "date": "2022-05-02",
            "name": "Morning"
          }
        ]
      }
    }
  },
  {
    "_id": "62626a7446ba9a911a623b37",
    "name": "Pinki Das",
    "shifts": {
      "_id": "62636ba4cbda6d2b17f5cae1",
      "month": "2022-05",
      "shift": {
        "date": "2022-05-01",
        "shiftId": [
          {
            "_id": "622bb0f4b88dc92e3c2cac56",
            "date": "2022-05-01",
            "name": "Day"
          }
        ]
      }
    }
  }
]

但我需要的数据应该如下所示。shiftId应该与班次数据一起嵌套在班次数组下。

{
        "_id": "621eedae92979fd8f0e9451d",
        "name": "Pallab Koley",
        "shifts": {
            "_id": "62636b9fcbda6d2b17f5cae0",
            "month": "2022-05",
            "shift": [
                {
                    "date": "2022-05-01",
                    "shiftId": [
                        {
                            "_id": "622bb0f4b88dc92e3c2cac56",
                            "date": "2022-05-01",
                            "name": "Day"
                        }
                    ]
                },
                {
                    "date": "2022-05-02",
                    "shiftId": [
                        {
                            "_id": "622b55f8f59dcdd1ab9b36b1",
                            "date": "2022-05-02",
                            "name": "Morning"
                        }
                    ]
                }
            ]
        }
    },
    {
        "_id": "62626a7446ba9a911a623b37",
        "name": "Pinki Das",
        "shifts": {
            "_id": "62636ba4cbda6d2b17f5cae1",
            "month": "2022-05",
            "shift": {
                "date": "2022-05-01",
                "shiftId": [
                    {
                        "_id": "622bb0f4b88dc92e3c2cac56",
                        "date": "2022-05-01",
                        "name": "Day"
                    }
                ]
            }
        }
    }
]

这是缺少班次下的日期字段。并且还需要对移位数组进行分组。请帮帮我。操场

YuTing

$set之后使用$lookup

db.employees.aggregate([
  {
    $lookup: {
      from: "shifts",
      localField: "shifts.shift.shiftId",
      foreignField: "_id",
      as: "shifts.shift2"
    }
  },
  {
    $set: {
      "shifts.shift": {
        $map: {
          input: "$shifts.shift",
          as: "s",
          in: {
            $mergeObjects: [
              "$$s",
              {
                shiftId: {
                  $filter: {
                    input: "$shifts.shift2",
                    as: "s2",
                    cond: { $eq: [ "$$s2._id", "$$s.shiftId" ] }
                  }
                }
              }
            ]
          }
        }
      }
    }
  },
  {
    $unset: [ "shifts.shift2" ]
  }
])

mongoplayground

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章