仅选择子文档或数组 [英] select only subdocuments or arrays

查看:74
本文介绍了仅选择子文档或数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
    "_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
                }
            ]
        }
    ]
}

在上述集合(即国家/地区)中,我只有一个文档,并且我想获取有关特定州的详细信息(让我们说"country.states.name":"orissa"),但是我想要我的结果如此处而不是整个文档.在Mogo中有没有办法...

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
        }
    ]
   }

谢谢

推荐答案

尝试过:

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

得到了:

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

这篇关于仅选择子文档或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆