Get result from mongodb using $ and and $ in

my schematic project - I want to get some information from mongodb

{
        "_id" : "23423q53q45345",       
        "value" : "5942178562002.65",   
        "dataset" : "GDP (current US$)",
        "data" : [
                { 
                        "data_name" : "country",
                        "value" : "india"
                },
                {
                        "data_name" : "date",
                        "value" : "2011"
                }
        ]
},  
{
        "_id" : "23423q53qdsfsd5",       
        "value" : "1234238562002.65",   
        "dataset" : "GDP (current US$)",
        "data" : [
                {
                        "data_name" : "country",
                        "value" : "india"
                },
                {
                        "data_name" : "date",
                        "value" : "2012"
                }
        ]
},
{
        "_id" : "213423q45345",       
        "value" : "6576867562002.65",   
        "dataset" : "GDP (current US$)",
        "data" : [
                { 
                        "data_name" : "country",
                        "value" : "us"
                },
                {
                        "data_name" : "date",
                        "value" : "2011"
                }
        ]
},
{
        "_id" : "4564564545dsfsd5",       
        "value" : "2354353462002.65",   
        "dataset" : "GDP (current US$)",
        "data" : [
                {
                        "data_name" : "country",
                        "value" : "us"
                },
                {
                        "data_name" : "date",
                        "value" : "2012"
                }
        ]
}

      

I want to get data from India for 2011

I used this query

db.collection.find({
    "data.value": {
        "$in": [
            "india","2011"
        ]
    }
});

      

it returns two results

{
        "_id" : "23423q53q45345",       
        "value" : "5942178562002.65",   
        "dataset" : "GDP (current US$)",
        "data" : [
                { 
                        "data_name" : "country",
                        "value" : "india"
                },
                {
                        "data_name" : "date",
                        "value" : "2011"
                }
        ]
},  
{
        "_id" : "23423q53qdsfsd5",       
        "value" : "1234238562002.65",   
        "dataset" : "GDP (current US$)",
        "data" : [
                {
                        "data_name" : "country",
                        "value" : "india"
                },
                {
                        "data_name" : "date",
                        "value" : "2012"
                }
        ]
}

      

it should be one of the results

{
        "_id" : "23423q53q45345",       
        "value" : "5942178562002.65",   
        "dataset" : "GDP (current US$)",
        "data" : [
                { 
                        "data_name" : "country",
                        "value" : "india"
                },
                {
                        "data_name" : "date",
                        "value" : "2011"
                }
        ]
}

      

I know the request is wrong, but how to achieve this please help me

+3


source to share


1 answer


db.collection.find({
    $and: [
        {"data.value": "india"},
        {"data.value": "2011"}
    ]
});

      



+1


source







All Articles