嵌套数组中的MongoDB查找

发布于 2025-01-23 15:20:03 字数 4003 浏览 0 评论 0原文

我正在尝试在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"
                    }
                ]
            }
        }
    }
]

这是丢失的日期字段。并且还需要对轮班阵列进行分组。请帮助我。 Playground

I am trying to make lookup in MongoDB nested array. My Data is looks like.

[
    {
        "_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"
                }
            ]
        }
    }
]

I was trying with the lookup.

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

And getting the result.

[
  {
    "_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"
          }
        ]
      }
    }
  }
]

But my require data should looks like as bellow. shiftId should nested under shift array along with shifts data.

{
        "_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"
                    }
                ]
            }
        }
    }
]

Here is date field under shift is missing. And also need to group the shift array. Please help me out. PlayGround

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ繥欢鉨o。 2025-01-30 15:20:03

$ 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" ]
  }
])

Use $set after $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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文