如何在查找管道内的嵌套数组中搜索 MongoDB

How to search in a nested array inside lookup pipeline MongoDB

约会示例数据

{
        "doctorId": "623f484709fb21a7760ce187",
        "userId": "6223370153c8126cd85884ce",
        "forUser": {
            "whom": "other",
            "id": "626249c4b666dc59628502f2"
        },
        "appointmentType": "walkin",
        "dates": {
            "createdAt": "2022-04-30T08:06:30.066Z",
            "forDateAndShift": {
                "date": "2022-05-02T00:00:00.000Z",
                "shift": "623f484709fb21a7760ce188"
            }
        },
        "status": "confirmed",
        "_id": "626cee063558d4a281fcccdb",
    }

医生样本数据

"name": "Someone",
    "profile": {
        "qualification": "MBBS",
        "speciality": "Cardiologist",
        "experience": 5
    },
    "timeSlots": [
        {
            "day": "3",
            "shifts": [
                {
                     _id:"623f484709fb21a7760ce188"
                    "maximumAllowedAppointments": 30,
                    "startTime": {
                        "hours": "9",
                        "minutes": "0"
                    },
                    "endTime": {
                        "hours": "12",
                        "minutes": "0"
                    }
                },
            ]
        },

想要获得与预约轮班 ID 相匹配的医生轮班时间,尝试使用管道并进行内部查找,但除了空数组外没有提供任何输出。 查询试图实现相同

appointment.aggregate([
  { $match: { userId: user._id } },
  { $sort: { "dates.forDateAndShift.date": 1 } },
  {
    $lookup: {
      from: "doctors",
      localField: "doctorId",
      foreignField: "_id",
      let: { shiftId: "$dates.forDateAndShift.shift" },
      pipeline: [
        { $match: { "timeSlots.shifts": { $elemMatch: { _id: "$$shiftId" } } } }
      ],
      as: "doctor"
    }
  },
  { $unwind: "$doctor" },
  {
    $project: {
      status: 1,
      forUser: 1,
      dates: 1,
      serialNumber: 1,
      "doctor.name": 1,
      appointmentType: 1,
      "doctor.profile": 1
    }
  },
  { $group: { _id: "$status", appointments: { $push: "$$ROOT" } } },
  { $project: { _id: 0, status: "$_id", appointments: "$appointments" } }
])

我写这个问题的这一部分是为了避免堆栈溢出的所有代码警告,因此请您忽略这部分

doctor 中获得右移的一种方法是:

db.appointment.aggregate([
  {
    $match: {userId: "6223370153c8126cd85884ce"}
  },
  {
    $sort: {"dates.forDateAndShift.date": 1}
  },
  {
    $lookup: {
      from: "doctors",
      let: {
        shiftId: "$dates.forDateAndShift.shift",
        doctorId: "$doctorId"
      },
      pipeline: [
        {
          $match: {$expr: {$eq: ["$_id", "$$doctorId"]}}
        },
        {
          $unwind: "$timeSlots"
        },
        {
          $project: {
            _id: 0,
            shifts: {
              $filter: {
                input: "$timeSlots.shifts",
                as: "item",
                cond: {$eq: ["$$item._id", "$$shiftId"]}
              }
            }
          }
        },
        {
          "$addFields": {"shiftsCount": {$size: "$shifts"}}
        },
        {
          $match: {shiftsCount: {$gt: 0}}
        },
        {
          $unset: "shiftsCount"
        }
      ],
      as: "doctor"
    }
  },
 // TODO: continue your query
])

你可以看到它是如何在这个 playground example 上工作的。

如果 shift_id 是唯一的,您可以继续 $lookup 管道:

{$project: {shifts: {$arrayElemAt: ["$shifts", 0]}}}