带有$ cond的mongo聚合不适用于嵌套查询的$ eq

时间:2019-12-21 04:00:56

标签: mongodb mongoose mongodb-query aggregation-framework

我在引用$cond语句中引用嵌套数组项时遇到问题。

db.getCollection('bookings').aggregate([
  {
    $lookup: {
      from: "listings",
      localField: "listingId",
      foreignField: "_id",
      as: "listing"
    }
  },
  {
    $match: {
      $and: [
        {
          locationId: ObjectId("5c0f0c882fcf07fb08890c27")
        },
        {
          $or: [
            {
              $and: [
                {
                  state: "booked"
                },
                {
                  startDate: {
                    $lte: new Date()
                  }
                },
                {
                  startDate: {
                    $gte: ISODate("2019-12-18T07:00:00.000Z")
                  }
                }
              ]
            },
            {
              $and: [
                {
                    listing: {
                        $elemMatch: { 
                            inspectionStatus: "none" 
                         }
                    }
                },
                {
                  endDate: {
                    $lte: new Date()
                  }
                },
                {
                  endDate: {
                    $gte: ISODate("2019-12-18T07:00:00.000Z")
                  }
                },
                {
                  state: {
                    $in: [
                      "active",
                      "returned"
                    ]
                  }
                }
              ]
            },
            {
              $and: [
                {
                  state: {
                    $ne: "cancelled"
                  }
                },
                {
                  $or: [
                    {
                      $and: [
                        {
                          startDate: {
                            $gte: ISODate("2019-12-20T07:00:00.993Z")
                          }
                        },
                        {
                          startDate: {
                            $lte: ISODate("2019-12-21T06:59:59.999Z")
                          }
                        }
                      ]
                    },
                    {
                      $and: [
                        {
                          endDate: {
                            $gte: ISODate("2019-12-20T07:00:00.993Z")
                          }
                        },
                        {
                          endDate: {
                            $lte: ISODate("2019-12-21T06:59:59.999Z")
                          }
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  },
  {
    $addFields: {
      isLate: {
        $cond: [
          {
            $or: [
              {
                $and: [
                  {
                    $eq: [
                        "$listing.0.inspectionStatus",
                      "none"
                    ]
                  },
                  {
                    $lte: [
                      "$endDate",
                      new Date()
                    ]
                  },
                  {
                    $gte: [
                      "$endDate",
                      ISODate("2019-12-18T07:00:00.000Z")
                    ]
                  },
                  {
                    $in: [
                      "$state",
                      [
                        "active",
                        "returned"
                      ]
                    ]
                  },

                ]
              },
              {
                $and: [
                  {
                    $eq: [
                      "$state",
                      "booked"
                    ]
                  },
                  {
                    $lte: [
                      "$startDate",
                      new Date()
                    ]
                  },
                  {
                    $gte: [
                      "$startDate",
                      ISODate("2019-12-18T07:00:00.000Z")
                    ]
                  }
                ]
              }
            ]
          },
          true,
          false
        ]
      }
    }
  }
])

在上面,$cond语句中的以下行根本不起作用:

$eq: [
   "$listing.0.inspectionStatus",
   "none"
]

我的问题是-如何进行上述工作?请注意,在查询之后,listing字段中始终只有一个数组项(那里不超过一个数组项)。我尝试了$listing.$0.$inspectionStatus之类的其他变体-但似乎没有任何效果。我可以沿着研究groupfilter的轨迹走下去,但是当我总是只想访问listing数组中的第一个也是唯一的项目时,我觉得这太过分了。

1 个答案:

答案 0 :(得分:1)

请在$ cond关键字

中使用 $ in 关键字代替 $ eq 关键字
db.demo1.aggregate([
      {
        $lookup: {
          from: "demo2",
          localField: "listingId",
          foreignField: "_id",
          as: "listing"
        }
      },
      {
        $match: {
          $and: [
            {
              locationId: ObjectId("5c0f0c882fcf07fb08890c27")
            },
            {
              $or: [
                {
                  $and: [
                    {
                      state: "booked"
                    },
                    {
                      startDate: {
                        $lte: new Date()
                      }
                    },
                    {
                      startDate: {
                        $gte: ISODate("2019-12-18T07:00:00.000Z")
                      }
                    }
                  ]
                },
                {
                  $and: [
                    {
                        listing: {
                            $elemMatch: { 
                                inspectionStatus: "none" 
                             }
                        }
                    },
                    {
                      endDate: {
                        $lte: new Date()
                      }
                    },
                    {
                      endDate: {
                        $gte: ISODate("2019-12-18T07:00:00.000Z")
                      }
                    },
                    {
                      state: {
                        $in: [
                          "active",
                          "returned"
                        ]
                      }
                    }
                  ]
                },
                {
                  $and: [
                    {
                      state: {
                        $ne: "cancelled"
                      }
                    },
                    {
                      $or: [
                        {
                          $and: [
                            {
                              startDate: {
                                $gte: ISODate("2019-12-20T07:00:00.993Z")
                              }
                            },
                            {
                              startDate: {
                                $lte: ISODate("2019-12-21T06:59:59.999Z")
                              }
                            }
                          ]
                        },
                        {
                          $and: [
                            {
                              endDate: {
                                $gte: ISODate("2019-12-20T07:00:00.993Z")
                              }
                            },
                            {
                              endDate: {
                                $lte: ISODate("2019-12-21T06:59:59.999Z")
                              }
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      },

      {
        $addFields: {
          isLate: {
            $cond: [
              {
                $or: [
                  {
                    $and: [
                      {
                        $in: [
                            "none",
                            "$listing.inspectionStatus",

                        ]
                      },
                      {
                        $lte: [
                          "$endDate",
                          new Date()
                        ]
                      },
                      {
                        $gte: [
                          "$endDate",
                          ISODate("2019-12-18T07:00:00.000Z")
                        ]
                      },
                      {
                        $in: [
                          "$state",
                          [
                            "active",
                            "returned"
                          ]
                        ]
                      },

                    ]
                  },
                  {
                    $and: [
                      {
                        $eq: [
                          "$state",
                          "booked"
                        ]
                      },
                      {
                        $lte: [
                          "$startDate",
                          new Date()
                        ]
                      },
                      {
                        $gte: [
                          "$startDate",
                          ISODate("2019-12-18T07:00:00.000Z")
                        ]
                      }
                    ]
                  }
                ]
              },
              true,
              false
            ]
          }
        }
      }
    ])