John
John

Reputation: 135

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

Upvotes: 6

Views: 14088

Answers (5)

T3db0t
T3db0t

Reputation: 3551

If you don't want to use aggregate, you can do it pretty easily at the application layer using underscore (included by default):

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

This will give you the entire state record that matches statename. Very convenient.

Upvotes: 1

shibi M
shibi M

Reputation: 11

db.countries.find({ "states":  { "$elemMatch": { "name": orissa }}},{"country" : 1, "states.$": 1 })

Upvotes: 1

marciowerner
marciowerner

Reputation: 518

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

Upvotes: 8

nnythm
nnythm

Reputation: 3320

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.

Upvotes: 5

Andrew Orsich
Andrew Orsich

Reputation: 53685

Any query in mongodb always return root document.

There is only one way for you to load one sub document with parent via $slice if you know ordinal number of state in nested array:

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

$slice work in default order (as documents was inserted in nested array). Also if you don't need fields from a country you can include only _id and states in result:

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

Then result document will looks like this one:

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

Upvotes: 2

Related Questions