mongodb - 从mongodb中的嵌套数组中只检索匹配的对象

标签 mongodb mongodb-query aggregation-framework

在这个 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"
          }
        ]
      }        ]
  }
]

提前致谢:)

最佳答案

你可以试试下面的聚合

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"
  }
]

关于mongodb - 从mongodb中的嵌套数组中只检索匹配的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824370/

相关文章:

node.js - Mongodb 更新所有具有唯一 id 的文档

javascript - MongoDB聚合忽略索引顺序

mongodb - Mongodb平均聚合查询不带组

java - Mongodb 异步 java 驱动程序 find()

mongodb - 将常规 MongoDB 查询运算符与 $or 运算符相结合

linux - Mongo 输出到 shell 变量

mongodb - $查找相同的集合

mongodb - mongo $ne 查询数组未按预期工作

c# - 对应用于 MongoDB 中数组元素中每个项目的函数进行排序

MongoDB - 有没有办法在哈希上执行 $set 而不覆盖现有/未引用的键/值?