select only subdocuments or arrays

后端 未结 5 703
情书的邮戳
情书的邮戳 2021-02-06 14:59
{
    \"_id\":{
        \"oid\":\"4f33bf69873dbc73a7d21dc3\"
    },
    \"country\":\"IND\",
    \"states\":[{
            \"name\":\"orissa\",
            \"direction\"         


        
相关标签:
5条回答
  • 2021-02-06 15:02

    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
    
    0 讨论(0)
  • 2021-02-06 15:02

    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.

    0 讨论(0)
  • 2021-02-06 15:06

    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.

    0 讨论(0)
  • 2021-02-06 15:08

    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
                    }
                ]
            }]
    }
    
    0 讨论(0)
  • 2021-02-06 15:12
    db.countries.find({ "states":  { "$elemMatch": { "name": orissa }}},{"country" : 1, "states.$": 1 })
    
    0 讨论(0)
提交回复
热议问题