【问题标题】:How can I filter query subdocuments using aggregate in Mongoose?如何在 Mongoose 中使用聚合过滤查询子文档?
【发布时间】:2022-01-18 10:48:10
【问题描述】:

我有以下文件信息。

[
  {
    _id: ObjectId("61d67f0a74ec8620f34c57ed"),
    shot: [
      {
        shotId: "undefinedMKSf*Tf#!qHxWpz1hPzUBTz%",
        clubType: "driver",
        shotCount: 20,
        unit: "meter",
        _id: ObjectId("61d67f0a74ec8620f34c57ef"),
        createdAt: ISODate("2022-01-06T05:32:58.391Z")
      },
      {
        shotId: "undefinedMKSf*Tf#!qHxWpz1hPzUBTz%",
        clubType: "wood",
        shotCount: 20,
        unit: "meter",
        _id: ObjectId("61d67f0a74ec8620f34c57f0"),
        createdAt: ISODate("2022-01-08T05:32:58.391Z")
      },
      {
        shotId: "undefinedMKSf*Tf#!qHxWpz1hPzUBTz%",
        clubType: "wood",
        shotCount: 15,
        unit: "yard",
        _id: ObjectId("61d67f0a74ec8620f34c57f0"),
        createdAt: ISODate("2022-01-08T05:32:58.391Z")
      },
      {
        shotId: "undefinedMKSf*Tf#!qHxWpz1hPzUBTz%",
        clubType: "wood",
        shotCount: 15,
        unit: "yard",
        _id: ObjectId("61d67f0a74ec8620f34c57f0"),
        createdAt: ISODate("2022-01-08T05:32:58.391Z")
      }
    ]
  }
]

在上面的文档中,如何才能只找到woodclubType15shotCountyardunit的文档?

我写了如下查询语句,但是没有出预期的结果。

db.collection.aggregate([
  {
    "$match": {
      _id: ObjectId("61d67f0a74ec8620f34c57ed"),
      "shot.clubType": "wood",
      shot: {
        $elemMatch: {
          $and: [
            {
              "createdAt": {
                $gte: ISODate("2022-01-07T05:32:58.391Z")
              }
            },
            {
              "createdAt": {
                $lte: ISODate("2022-01-09T05:32:58.391Z")
              }
            }
          ]
        }
      }
    }
  },
  {
    "$set": {
      shot: {
        "$filter": {
          "input": "$shot",
          "as": "s",
          "cond": {
            $and: [
              {
                "$eq": [
                  "$$s.clubType",
                  "wood"
                ]
              },
              {
                "$gte": [
                  "$$s.createdAt",
                  ISODate("2022-01-07T05:32:58.391Z")
                ]
              },
              {
                "$lte": [
                  "$$s.createdAt",
                  ISODate("2022-01-09T05:32:58.391Z")
                ]
              }
            ]
          }
        }
      }
    }
  }
])

如何修改查询语句?

【问题讨论】:

    标签: database mongodb mongoose


    【解决方案1】:

    在聚合末尾添加$project

    db.collection.aggregate([
      {
        "$match": {
          _id: ObjectId("61d67f0a74ec8620f34c57ed"),
          "shot.clubType": "wood",
          shot: {
            $elemMatch: {
              $and: [
                {
                  "createdAt": {
                    $gte: ISODate("2022-01-07T05:32:58.391Z")
                  }
                },
                {
                  "createdAt": {
                    $lte: ISODate("2022-01-09T05:32:58.391Z")
                  }
                }
              ]
            }
          }
        }
      },
      {
        "$set": {
          shot: {
            "$filter": {
              "input": "$shot",
              "as": "s",
              "cond": {
                $and: [
                  {
                    "$eq": [
                      "$$s.clubType",
                      "wood"
                    ]
                  },
                  {
                    "$gte": [
                      "$$s.createdAt",
                      ISODate("2022-01-07T05:32:58.391Z")
                    ]
                  },
                  {
                    "$lte": [
                      "$$s.createdAt",
                      ISODate("2022-01-09T05:32:58.391Z")
                    ]
                  }
                ]
              }
            }
          }
        }
      },
      {
        "$project": {
          "shot.clubType": 1,
          "shot.shotCount": 1,
          "shot.unit": 1
        }
      }
    ])
    

    mongoplayground

    【讨论】:

    • 通过添加$eq 的使用,我实际上得到了想要的结果。谢谢。
    猜你喜欢
    • 2020-08-02
    • 2021-07-04
    • 2016-08-29
    • 2023-03-23
    • 2016-03-24
    • 2019-07-13
    • 2014-01-22
    • 2013-06-08
    • 2020-04-24
    相关资源
    最近更新 更多