Mongodb – how to query nested json objects in mongodb

jsonmongodb

{
    "_id" : ObjectId("58c7da2adaa8d031ea699fff"),
    "title" : "sdsddfasssdsdfsdffdf",
    "viewData" : [ 
        {
            "_id" : ObjectId("58c7f9d03ef6c93ce964d19a"),
            "widgetData" : {
                "pageIndex" : 5,
                "pageLength" : 5,
                "fetch" : 239,
                "granularity" : "day",
                "threshold" : [ 
                    {
                        "temperature" : {
                            "between" : {
                                "critical" : "40",
                                "warning" : "10"
                            },
                            "notification" : "Email"
                        }
                    }, 
                    {
                        "dewPoint" : {
                            "between" : {
                                "critical" : "40",
                                "warning" : "10"
                            },
                            "notification" : "Email"
                        }
                    }, 
                    {
                        "visibility" : {
                            "between" : {
                                "critical" : "40",
                                "warning" : "10"
                            },
                            "notification" : "Email"
                        }
                    }
                ],
                "measures" : [ 
                    "visibility", 
                    "dewPoint", 
                    "temperature"
                ],
                "dimensions" : [ 
                    "country", 
                    "name", 
                    "lat", 
                    "lon"
                ],
                "hierarchies" : {
                    "country" : {
                        "lon" : [ 
                            "-3.056", 
                            "-0.502", 
                            "-2.1955", 
                            "-2.6"
                        ],
                        "lat" : [ 
                            "53.497", 
                            "53.031", 
                            "49.44", 
                            "49.2079"
                        ],
                        "name" : [ 
                            "CROSBY", 
                            "CRANWELL", 
                            "JERSEY", 
                            "GUERNSEY"
                        ],
                        "country" : [ 
                            "ENGLAND", 
                            "CHANNEL ISLANDS"
                        ]
                    }
                },
                "aggregation" : {
                    "temperature" : "MIN",
                    "dewPoint" : "MAX",
                    "visibility" : "AVG"
                },
                "widget" : {
                    "mode" : "absolute value",
                    "widgetType" : "MonitorWidget",
                    "title" : "England",
                    "description" : ""
                },
                "pinned" : true,
                "DataSource" : "MetadataDataSource",
                "mid" : "589414f51163d131285563e2"
            }
        }

how to find widget title value?

i tried

db.getCollection('mobiledashboards').find({"_id": "58c7da2adaa8d031ea699fff" },{ viewData: { $elemMatch : { "widgetData.widget.title": "England" } } })

but not working, how to solve this one.

Best Answer

I found the answer myself:

db.getCollection('mobiledashboards')
    .find
    (
        {
            "_id": ObjectId("58c7da2adaa8d031ea699fff") 
        },
        { 
            viewData: { $elemMatch : { "widgetData.widget.title" : "England" } }
        }
    )