在Mongodb中查询嵌套的Json并获取所有结果

时间:2018-11-18 13:01:04

标签: json mongodb

我在mongo db中有一个嵌套的Json文档,如下所示: / * 1 * /

{
    "_id" : ObjectId("5bf159cc6bf6ab0ac374f80c"),
    "name" : "Jack",
    "age" : "30",
    "info" : {
        "0" : {
            "status" : "true",
            "name" : "luffy"
        },
        "1" : {
            "status" : "true",
            "name" : "sanji"
        },
        "2" : {
            "status" : "false",
            "name" : "zoro"
        }
    }
}

/* 2 */
{
    "_id" : ObjectId("5bf15f286bf6ab0ac374f8ed"),
    "name" : "Mack",
    "age" : "33",
    "info" : {
        "0" : {
            "status" : "true",
            "name" : "naruto"
        },
        "1" : {
            "status" : "true",
            "name" : "sakura"
        },
        "2" : {
            "status" : "false",
            "name" : "sasuke"
        }
    }
}

现在我想做的就是查询并获取status ='true'的那些结果。经过一番谷歌搜索之后,我知道了如何查询嵌套文档并提出了示例查询:

db.getCollection('test').find({"info.0.status":"true"})

但是从上面的查询中您知道,该查询只会从第0个数组中获取适当的结果,如何获取查询以遍历数组并返回带有“ status”:“ true”的文档。是Mongodb的新手,请忽略任何错误。

1 个答案:

答案 0 :(得分:1)

如果您查询db.getCollection('test').find({"info.0.status":"true"}),它将返回为:

{
    "_id" : ObjectId("5bf159cc6bf6ab0ac374f80c"),
    "name" : "Jack",
    "age" : "30",
    "info" : {
        "0" : {
            "status" : "true",
            "name" : "luffy"
        },
        "1" : {
            "status" : "true",
            "name" : "sanji"
        },
        "2" : {
            "status" : "false",
            "name" : "zoro"
        }
    }
}

/* 2 */
{
    "_id" : ObjectId("5bf15f286bf6ab0ac374f8ed"),
    "name" : "Mack",
    "age" : "33",
    "info" : {
        "0" : {
            "status" : "true",
            "name" : "naruto"
        },
        "1" : {
            "status" : "true",
            "name" : "sakura"
        },
        "2" : {
            "status" : "false",
            "name" : "sasuke"
        }
    }
}

但是,如要求所述,您需要将数据建模为:

[
  {
    "_id": ObjectId("5bf159cc6bf6ab0ac374f80c"),
    "name": "Jack",
    "age": "30",
    "info": [
      {
        "status": "true",
        "name": "luffy"
      },
      {
        "status": "true",
        "name": "sanji"
      },
      {
        "status": "false",
        "name": "zoro"
      }
    ]
  },
  {
    "_id": ObjectId("5bf15f286bf6ab0ac374f8ed"),
    "name": "Mack",
    "age": "33",
    "info": [
      {
        "status": "true",
        "name": "naruto"
      },
      {
        "status": "true",
        "name": "sakura"
      },
      {
        "status": "false",
        "name": "sasuke"
      }
    ]
  }
]

使用$filter,它将返回预期结果:

db.collection.aggregate([
  {
    $project: {
      items: {
        $filter: {
          input: "$info",
          as: "info",
          cond: {
            $eq: [
              "$$info.status",
              "true"
            ]
          }
        }
      }
    }
  }
])

示例响应:

[
  {
    "_id": ObjectId("5bf159cc6bf6ab0ac374f80c"),
    "items": [
      {
        "name": "luffy",
        "status": "true"
      },
      {
        "name": "sanji",
        "status": "true"
      }
    ]
  },
  {
    "_id": ObjectId("5bf15f286bf6ab0ac374f8ed"),
    "items": [
      {
        "name": "naruto",
        "status": "true"
      },
      {
        "name": "sakura",
        "status": "true"
      }
    ]
  }
]