具有多个$ expr参数的Mongo $ lookup $ pipeline在$ lookup结果中未返回正确的数据

安德鲁·泰勒(Andrew Taylor)

我有两个mongo集合,一个包含约会提醒,另一个包含通知。我正在尝试返回所有存档的结果:给定branchId / climate_id中的虚假提醒,并包括其已确认:虚假通知。另外,无论是否有通知,我都希望确保约会显示在结果中。

我需要通过branchId aka clinic_id“加入”集合,然后为每个结果约会提醒创建一个未确认通知的数组。

我返回正确的约会,但notif数组未通过匹配PatientId / Patient_id进行过滤每个提醒似乎都包含完全相同的notif数组。除此之外,其他一切似乎都是正确的。因此,我的问题是如何确保notif数组仅包含与提醒的Patient_id值匹配的PatientId?

截断的约会提醒架构:

      {
      time: {
        type: Date,
        required: true
      },
  patient_id: {
    type: String,
    required: true
  },
      status: {
        type: String,
        default: 'unconfirmed'
      },
      archive: {
        type: Boolean,
        default: false
      },
      branch: [
    // Incorrectly setup as an array rather than an object. 
    // Can $unwind as there is only ever one item in the array
        {
          name: {
            type: String,
            required: true
          },
          clinic_id: { // aka branchId
            type: String,
            required: true
          }
        }
      ]
    }

通知架构:

{
  branchId: { type: String, required: true }, // aka clinic_id
  patientId: { type: String, required: true },
  acknowledged: { type: Boolean, default: false },
  date: { type: Date, default: Date.now }
}

汇总查询:

[
  { $match: { 'branch.0.clinic_id': '1', archive: false } },
  { $unwind: '$branch' },
  {
    $lookup: {
      from: 'notifications',
      let: { clinic_id: '1', patient_id: '$patientId' }, //<-- issue with patient_id?
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
               { $eq: ["$patientId", "$$patientId"] }, <-- errors $$patientId unknown value. $$patient_id returns 0 results.
                { $eq: ['$branchId', '$$clinic_id'] },
                { $eq: ['$acknowledged', false] }
              ]
            }
          }
        }
      ],
      as: 'notif'
    }
  }
]

示例输出,其中包含有关我期望的输出和不正确输出的注释:

  {
  patient_id: '1',
  time: '2019-05-29T11:00:00.000Z',
  status: 'unconfirmed',
  archive: false,
  branch: [
    {
      name: 'Example location',
      clinic_id: '100',
    }
  ],
  notif: [
  {
    // This is correct
    branchId: '100', // branchId matches clinic_id
    patientId: '1',  // patientId matches contacts patient_id
    acknowledged: false, // notification is unacknowledged
    date: '2019-05-18T16:18:05.480Z'
  },
  {
   // This is not correct
    branchId: '100', 
    patientId: '2', // PatientId does not match patient_id of reminder
    acknowledged: false,
    date: '2019-05-20T16:18:05.480Z'
  }
  ]
}
安德鲁·泰勒(Andrew Taylor)

借助tom slabbaert,此问题得以解决:

[
        { $match: { 'branch.clinic_id': '1', archive: false } },
        { $unwind: '$branch' },
        {
          $lookup: {
            from: 'notifications',
            let: { clinic_id: '1', patient_id: '$patient_id' }, // <-- changed here
            pipeline: [
              {
                $match: {
                  $expr: {
                    $and: [
                      { $eq: ["$$patient_id", "$patientId"]}, // <-- changed here
                      { $eq: ['$branchId', '$$clinic_id'] },
                      { $eq: ['$acknowledged', false] }
                    ]
                  }
                }
              }
            ],
            as: 'notif'
          }
        }
      ]

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章