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 the collection above (i.e. countries), I have only one document and I want to get information about a specific state (say, "country.states.name": "orissa"), but I want my result to be here instead of the whole 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
        }
    ]
   }

thank

+5
source share
5 answers

Tried this:

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

And received:

{
    "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
+7
source

You cannot do it right now, but you can relax in the aggregation structure with $ . You can try it now with experimental branch 2.1, the stable version will be released in 2.2, probably in a few months.

+5
source

mongodb .

$slice, :

// skip ordinalNumberOfState -1, limit 1
db.countries.find({_id: 1}, {states:{$slice: [ordinalNumber -1 , 1]}}) 

$slice ( ). , , _id :

db.countries.find({_id: 1}, {states:{$slice: [ordinalNumber -1 , 1]}, _id: 1}) 

:

{
    "_id":{
        "oid":"4f33bf69873dbc73a7d21dc3"
    },
    "states":[{
            "name":"orissa",
            "direction":"east",
            "population":41947358,
            "districts":[{
                    "name":"puri",
                    "headquarter":"puri",
                    "population":1498604
                },
                {
                    "name":"khordha",
                    "headquarter":"bhubaneswar",
                    "population":1874405
                }
            ]
        }]
}
+2
db.countries.find({ "states":  { "$elemMatch": { "name": orissa }}},{"country" : 1, "states.$": 1 })
+1

aggregate, , ( ):

var country = Groops.findOne({"property":value);
var state _.where(country, {"state":statename});

, statename. .

0

All Articles