Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select only subdocuments or arrays

{
    "_id":{
        "oid":"4f33bf69873dbc73a7d21dc3"
    },
    "country":"IND",
    "states":[{
            "name":"orissa",
            "direction":"east",
            "population":41947358,
            "districts":[{
                    "name":"puri",
                    "headquarter":"puri",
                    "population":1498604
                },
                {
                    "name":"khordha",
                    "headquarter":"bhubaneswar",
                    "population":1874405
                }
            ]
        },
        {
            "name":"andhra pradesh",
            "direction":"south",
            "population":84665533,
            "districts":[{
                    "name":"rangareddi",
                    "headquarter":"hyderabad",
                    "population":3506670
                },
                {
                    "name":"vishakhapatnam",
                    "headquarter":"vishakhapatnam",
                    "population":3789823
                }
            ]
        }
    ]
}

In above collection(i.e countries) i have only one document , and i want to fetch the details about a particular state (lets say "country.states.name" : "orissa" ) ,But i want my result as here under instead of entire document .Is there a way in Mogo...

     {
    "name": "orissa",
    "direction": "east",
    "population": 41947358,
    "districts": [
        {
            "name": "puri",
            "headquarter": "puri",
            "population": 1498604
        },
        {
            "name": "khordha",
            "headquarter": "bhubaneswar",
            "population": 1874405
        }
    ]
   }

Thanks

like image 502
John Avatar asked Feb 15 '12 07:02

John


2 Answers

Tried this:

db.countries.aggregate(      
    {
        "$project": {
            "state": "$states",
            "_id": 0
        }
    },
    {
        "$unwind": "$state"
    },
    {
        "$group": {
            "_id": "$state.name",
            "state": {
                "$first": "$state"
            }
        }
    },
    {
        "$match": {
            "_id": "orissa"
        }
    }
);

And got:

{
    "result" : [
            {
                    "_id" : "orissa",
                    "state" : {
                            "name" : "orissa",
                            "direction" : "east",
                            "population" : 41947358,
                            "districts" : [
                                    {
                                            "name" : "puri",
                                            "headquarter" : "puri",
                                            "population" : 1498604
                                    },
                                    {
                                            "name" : "khordha",
                                            "headquarter" : "bhubaneswar",
                                            "population" : 1874405
                                    }
                            ]
                    }
            }
    ],
    "ok" : 1
like image 111
marciowerner Avatar answered Oct 06 '22 00:10

marciowerner


You can't do it right now, but you will be able to with $unwind in the aggregation framework. You can try it now with the experimental 2.1 branch, the stable version will come out in 2.2, probably in a few months.

like image 24
nnythm Avatar answered Oct 06 '22 02:10

nnythm