从Document中的嵌套Object Array中提取MongoDB中的特定对象

时间:2018-01-11 18:43:59

标签: mongodb mongodb-query

我在这个Structure中有MongoDB中的这个集合。现在我需要查询这个数据以获得权重为60的relatedPeople。我搜索了这个,但发现特定的大多数查询使用$符号提供数据,但它给你rootnode但不是嵌套节点正在加工的信息。

{
        "_id": "7686348264868327",
        "Name": "myName",
        "phoneNo": "12434576896",
        "ExtraDetails": {
            "TotalPeople": 10,
            "activePeople": 8,
            "lostPeople": 2,
            "relatedPeople": [{
                    "Name": "reev",
                    "Relation": "Father",
                    "Weight": 60
                },
                {
                    "Name": "magen2",
                    "Relation": "Mother",
                    "Weight": 60
                },
                {
                    "Name": "neo",
                    "Relation": "Gardian",
                    "Weight": 70
                }

            ]
        }
    }
    {
        "_id": "76866898698798",
        "Name": "myName2",
        "phoneNo": "867867868",
        "ExtraDetails": {
            "TotalPeople": 8,
            "activePeople": 6,
            "lostPeople": 2,
            "relatedPeople": [{
                    "Name": "amazing",
                    "Relation": "Father",
                    "Weight": 60
                },
                {
                    "Name": "caring",
                    "Relation": "Mother",
                    "Weight": 90
                },
                {
                    "Name": "neo",
                    "Relation": "Gardian",
                    "Weight": 75
                }

            ]
        }
    }

输出应该是这样的东西

"relatedPeople":[
{
                        "Name": "reev",
                        "Relation": "Father",
                        "Weight": 60
                    },
                    {
                        "Name": "magen2",
                        "Relation": "Mother",
                        "Weight": 60
                    },
                    {
                        "Name": "amazing",
                        "Relation": "Father",
                        "Weight": 60
                    }

]

或者

[{
                            "Name": "reev",
                            "Relation": "Father",
                            "Weight": 60
                        },
                        {
                            "Name": "magen2",
                            "Relation": "Mother",
                            "Weight": 60
                        },
                        {
                            "Name": "amazing",
                            "Relation": "Father",
                            "Weight": 60
                        }]

1 个答案:

答案 0 :(得分:1)

您可以尝试以下解决方案:

db.collection.aggregate([
    { $match: { "ExtraDetails.relatedPeople.Weight": 60 }  },
    { $project: { "ExtraDetails.relatedPeople": 1 } },
    { $unwind: "$ExtraDetails.relatedPeople" },
    { $match: { "ExtraDetails.relatedPeople.Weight": 60 }  },
    { $replaceRoot: { newRoot: "$ExtraDetails.relatedPeople" } }
])

我使用$unwind在$ match和$replaceRoot中将relatedPeople作为单独的文档进行查询以摆脱嵌套。

或者,要获得第一个预期形状,您可以使用$ group with $ push:

db.collection.aggregate([
    { $match: { "ExtraDetails.relatedPeople.Weight": 60 }  },
    { $project: { "ExtraDetails.relatedPeople": 1 } },
    { $unwind: "$ExtraDetails.relatedPeople" },
    { $match: { "ExtraDetails.relatedPeople.Weight": 60 }  },
    {
       $group: {
         _id: null,
         relatedPeople: { $push: "$ExtraDetails.relatedPeople" }
       }
    },
    { $project: { _id: 0 } }
])
相关问题