仅选择子文档或数组

Joh*_*ohn 5 mongoose mongodb

{
    "_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
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

在上面的集合(即国家)我只有一个文档,我想获取有关特定状态的详细信息(让我们说"country.states.name":"orissa"),但我希望我的结果在这里而不是整个文件.在Mogo有办法......

     {
    "name": "orissa",
    "direction": "east",
    "population": 41947358,
    "districts": [
        {
            "name": "puri",
            "headquarter": "puri",
            "population": 1498604
        },
        {
            "name": "khordha",
            "headquarter": "bhubaneswar",
            "population": 1874405
        }
    ]
   }
Run Code Online (Sandbox Code Playgroud)

谢谢

mar*_*ner 7

试过这个:

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

得到了:

{
    "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
Run Code Online (Sandbox Code Playgroud)


nny*_*thm 5

你现在不能这样做,但你可以在聚合框架中使用$ unwind .您现在可以尝试使用实验2.1分支,稳定版本将在2.2中出现,可能在几个月内.


And*_*ich 2

mongodb 中的任何查询始终返回根文档。

如果您知道嵌套数组中的状态序号,则只有一种方法可以通过$slice加载带有父文档的子文档:

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

$slice 按默认顺序工作(因为文档被插入到嵌套数组中)。此外,如果您不需要某个国家/地区的字段,您可以仅在结果中包含 _id 和状态:

db.countries.find({_id: 1}, {states:{$slice: [ordinalNumber -1 , 1]}, _id: 1}) 
Run Code Online (Sandbox Code Playgroud)

然后结果文档将如下所示:

{
    "_id":{
        "oid":"4f33bf69873dbc73a7d21dc3"
    },
    "states":[{
            "name":"orissa",
            "direction":"east",
            "population":41947358,
            "districts":[{
                    "name":"puri",
                    "headquarter":"puri",
                    "population":1498604
                },
                {
                    "name":"khordha",
                    "headquarter":"bhubaneswar",
                    "population":1874405
                }
            ]
        }]
}
Run Code Online (Sandbox Code Playgroud)