Mohammed Gadiwala February 2016

mongodb query for results

[
    {
        "userid": "abcde",
        "dates": {
            "2-01-2015": {
                "9-10": {
                    "ava": "yes",
                    "bookibg_id": "null"
                },
                "10-11": {
                    "ava": "yes",
                    "bookibg_id": "null"
                }
            },
            "3-01-2015": {
                "9-10": {
                    "ava": "no",
                    "bookibg_id": "null"
                },
                "10-11": {
                    "ava": "no",
                    "bookibg_id": "null"
                }
            }
        }
    },
    {
        "userid": "abcde",
        "dates": {
            "2-01-2015": {
                "9-10": {
                    "ava": "yes",
                    "bookibg_id": "null"
                },
                "10-11": {
                    "ava": "no",
                    "bookibg_id": "null"
                }
            },
            "3-01-2015": {
                "9-10": {
                    "ava": "no",
                    "bookibg_id": "null"
                },
                "10-11": {
                    "ava": "no",
                    "bookibg_id": "null"
                }
            }
        }
    }
]

I am relatively new to nosql db and mongodb.Its kind of confusing how to query these. for the given db how do i write query to generate results: Query:List userids such that at "2-01-2015",the "ava" is "yes" at "9-10". Plzz also explain the results.

Answers


Narayansingh Rajput February 2016

you can find using

db.coll.find({'dates.2-01-2015.9-10.ava': "yes"},{"userid":1,'_id':0})

Here as 'dates.2-01-2015.9-10.ava':'yes' is used to find the value of ava as yes similary accessing a value from JSON objects and second argument is used to get only userid from query {"userid":1,'_id':0}

the output will be

{ "userid" : "abcde" }
{ "userid" : "abcde" }

if you remove second argument you'll get entire document in your result based on your second argument you can filter the feilds that you want

Post Status

Asked in February 2016
Viewed 3,633 times
Voted 6
Answered 1 times

Search




Leave an answer