MongoDb从深层嵌套的数组文档中获取所有匹配数据

时间:2017-11-21 05:59:53

标签: mongodb nested

如何获得2006年家庭中每个家庭成员的教育数据(包括家长)?我在mongodb中有数据:

{
  "name": "Families",
  "size": 3,
  "families": [
    {
      "name": "Johny's Family",
      "size": 2,
      "family_member": [
        {
          "name": "Ruben",
          "age": 22,
          "education": [
            {
              "school": "Edward Academy",
              "year": 2003
            }
          ]
        },
        {
          "name": "Hana",
          "age": 20,
          "education": [
            {
              "school": "Edward Academy",
              "year": 2006
            },
            {
              "school": "Nanyang University",
              "year": 2012
            }
          ]
        }
      ]
    },
    {
      "name": "Boy's Family",
      "size": 1,
      "family_member": [
        {
          "name": "Boy",
          "age": 23,
          "education": [
            {
              "school": "Broklyn Academy",
              "year": 2003
            },
            {
              "school": "Home School",
              "year": 2006
            }
          ]
        }
      ]
    }
  ]
}

我试图在mongodb中使用普通的find函数来获取它,但结果并不是我想要的。这是我的mongo脚本:

db.getCollection('tester').find(
{
    "name":"Families",
    "families.family_member.education.year":2006
},
{
    "families.$.family_member.education.year":1
}
)

任何人都可以建议最好的方法来获取数据:

{
  "name": "Families",
  "size": 3,
  "families": [
    {
      "name": "Johny's Family",
      "size": 2,
      "family_member": [
        {
          "name": "Hana",
          "age": 20,
          "education": [
            {
              "school": "Edward Academy",
              "year": 2006
            }
          ]
        }
      ]
    },
    {
      "name": "Boy's Family",
      "size": 1,
      "family_member": [
        {
          "name": "Boy",
          "age": 23,
          "education": [
            {
              "school": "Home School",
              "year": 2006
            }
          ]
        }
      ]
    }
  ]
}

4 个答案:

答案 0 :(得分:0)

您可以使用JavaScript代码处理数据,因为mongodb以Document为单位返回数据。 像这样处理“家庭”字段:

doc.families.map((family)=>{
    family.family_member = family.family_member.map((member)=>{
        member.education = member.education.filter((edu)=>edu.year==2006);
        return member;
    });
    return family;
})

答案 1 :(得分:0)

使用聚合管道我们可以得到这个结果。要实现此目的,请使用$unwind$match。 $ unwind需要使用两次,因为我们有深层嵌套的数组

db.tester.aggregate([
   {$unwind:"$families"}, 
   {$unwind:"$families.family_member"}, 
   {$unwind:"$families.family_member.education"}, 
   {$match:{"families.family_member.education.year":2006}}
])

答案 2 :(得分:0)

你可以尝试这个。您可以使用aggregation获得预期结果,然后必须使用$unwind,然后使用$match$group

db.CollectionName.aggregate([
  {$unwind: "$families"},
  {$unwind: "$families.family_member"},
  {$unwind: "$families.family_member.education"},
  {$match: {"families.family_member.education.year": 2006}},
  {
    $group: {
      _id: "$_id",
      name: {$first: "$name"},
      size: {$first: "$size"},
      families: {$push: "$families"}
    }
  }
])

答案 3 :(得分:0)

更改您的查询,如下所示,它应该有效: -

db.getCollection('tester').find({"families.family_member.education.year": 2006},{"families.family_member.education.year":1})

如果要打印全部内容,请仅使用: -

db.getCollection('tester').find({"families.family_member.education.year": 2006}

感谢。