Shivam Sharma
Shivam Sharma

Reputation: 325

Extracting Particular object from MongoDB from nested Object Array in Document

I have this collection in MongoDB in this Structure.Now I need to query the on this data to get the relatedPeople with weight 60. I searched on this but found noting specific most query give data using $ symbol but it gives you rootnode but not the info that which nested node is maching.

{
        "_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
                }

            ]
        }
    }

The Output Should be Something Like This

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

]

Or

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

Upvotes: 1

Views: 282

Answers (1)

mickl
mickl

Reputation: 49945

You can try following solution:

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

I'm using $unwind to query relatedPeople as a separate documents in $match and $replaceRoot to get rid of nestings.

Alternatively, to get first expected shape you can use $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 } }
])

Upvotes: 1

Related Questions