簡體   English   中英

MongoDB 在多個 label 嵌套數組中查找

[英]MongoDB lookup in multi label nested array

我是 MongoDB 的新手。我正在嘗試在多個 label 嵌套數組中進行查找。 我的數據如下所示。

[
    {
        "_id": "621eedae92979fd8f0e9451d",
        "name": "Pallab Koley",
        "shifts": {
            "_id": "62636b9fcbda6d2b17f5cae0",
            "month": "2022-05",
            "shift": [
                {
                    "date": "2022-05-01",
                    "shiftId": "622bb0f4b88dc92e3c2cac56"
                }
            ]
        }
    },
    {
        "_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": {
                "shiftId": [
                    {
                        "_id": "622bb0f4b88dc92e3c2cac56",
                        "name": "Day"
                    }
                ]
            }
        }
    },
    {
        "_id": "62626a7446ba9a911a623b37",
        "name": "Pinki Das",
        "shifts": {
            "_id": "62636ba4cbda6d2b17f5cae1",
            "month": "2022-05",
            "shift": {
                "shiftId": [
                    {
                        "_id": "622bb0f4b88dc92e3c2cac56",
                        "name": "Day"
                    }
                ]
            }
        }
    }
]

但我的期望數據應該如下所示。

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

這是shifts.shift下的date字段丟失。 shiftId正在替換 shift 數組下的所有字段。 請幫幫我。 mongo游樂場

您正在使用查找結果覆蓋數組的原始內容。 考慮使用子管道將日期存儲為變量並將其分配給查找結果。

db.employees.aggregate([
  {
    "$unwind": "$shifts.shift"
  },
  {
    "$lookup": {
      "from": "shifts",
      "let": {
        shiftDate: "$shifts.shift.date",
        sid: "$shifts.shift.shiftId"
      },
      "pipeline": [
        {
          "$match": {
            $expr: {
              "$eq": [
                "$_id",
                "$$sid"
              ]
            }
          }
        },
        {
          "$addFields": {
            "date": "$$shiftDate"
          }
        }
      ],
      "as": "shifts.shift.shiftId"
    }
  }
])

這是供您參考的Mongo playground

對不起,我在顯示我的要求時犯了一個錯誤。 我已經改變了多輪班的運動場。 修改過的游樂場 結果應該如下所示。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM