【问题标题】:Retrieve only matched object from nested array in mongodb从 mongodb 的嵌套数组中仅检索匹配的对象
【发布时间】:2018-06-13 17:49:47
【问题描述】:

在这个 json 中,我需要一个查找查询来查找“status”:“Y”的所有字段,如果父字段有“status”:“N”,则忽略子字段,否则查找子字段其中 "status":"Y" 及其父字段

注意:子字段在数组中

[
  {
    "type": "Type 1",
    "status": "Y",
    "code": "1",
    "category": [
      {
        "type": "Cat 1",
        "status": "Y",
        "code": "1000",
        "subcategories": [
          {
            "type": "Sub 1",
            "status": "N",
            "code": "1001"
          },
          {
            "type": "Sub 2",
            "status": "N",
            "code": "1002"
          },
          {
            "type": "Sub 3",
            "status": "Y",
            "code": "1003"
          }
        ]
      },
      {
        "type": "Cat 2",
        "status": "N",
        "code": "2000",
        "subcategories": [
          {
            "type": "Sub 4",
            "status": "Y",
            "code": "2001"
          },
          {
            "type": "Sub 5",
            "status": "Y",
            "code": "2002"
          }
         ]
      }
    ]
  }
]

我的输出应该是这样的

[
  {
    "type": "Type 1",
    "status": "Y",
    "code": "1",
    "category": [
      {
        "type": "Cat 1",
        "status": "Y",
        "code": "1000",
        "subcategories": [
          {
            "type": "Sub 3",
            "status": "Y",
            "code": "1003"
          }
        ]
      }        ]
  }
]

提前致谢:)

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    你可以试试下面的聚合

    db.collection.aggregate([
      { "$match": { "status": "Y" }},
      { "$unwind": "$category" },
      { "$match": { "category.status": "Y" } },
      { "$project": { "type": 1, "status": 1, "code": 1,
        "category.type": "$category.type",
        "category.status": "$category.status",
        "category.code": "$category.code",
        "category.subcategories": {
          "$filter": {
            "input": "$category.subcategories",
            "as": "subcategory",
            "cond": {
              "$eq": [
                "$$subcategory.status",
                "Y"
              ]
            }
          }
        }
      }},
      { "$group": {
        "_id": "$_id",
        "type": { "$first": "$type" },
        "status": { "$first": "$status" },
        "code": { "$first": "$code" },
        "category": { "$push": "$category" }
      }}
    ]).then((data) => {
      res.send(data)
    })
    

    为您提供以下输出(检查 here

    [
      {
        "_id": ObjectId("5a934e000102030405000000"),
        "category": [
          {
            "code": "1000",
            "status": "Y",
            "subcategories": [
              {
                "code": "1003",
                "status": "Y",
                "type": "Sub 3"
              }
            ],
            "type": "Cat 1"
          }
        ],
        "code": "1",
        "status": "Y",
        "type": "Type 1"
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多