Mongodb mongo聚合与$cond在嵌套查找时不与$eq一起工作

Mongodb mongo聚合与$cond在嵌套查找时不与$eq一起工作,mongodb,mongoose,mongodb-query,aggregation-framework,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我在引用$cond语句中的嵌套数组项时遇到问题 db.getCollection('bookings').aggregate([ { $lookup: { from: "listings", localField: "listingId", foreignField: "_id", as: "listing" } }, { $match: { $and: [ { l

我在引用
$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.$0.$inspectionStatus
——但似乎没有任何效果。我可以沿着搜索
过滤器
的轨道前进,但我觉得这太过分了,因为我总是想访问
列表中的第一个也是唯一一个项目
数组。

请使用$in关键字而不是$cond关键字中的$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
            ]
          }
        }
      }
    ])

请在@Mahesh output共享输入和输出数据:至于输入-请参阅输出-和
列表
列表
集合中的列表文档,其余的输出只是
预订
集合中的文档请共享预订和列表好的-这里是列表:然后预订:请告诉listingId在ListingJSonTank中的键在哪里@Mahesh-这就做到了!请注意,如果确实需要在聚合表达式中引用数组的第零个成员,则语法为
{$arrayElemAt:[“$array”,0]}
,对于其他有序元素也是如此。