简体   繁体   中英

Retrieve Result from mongodb using $and and $in

my schema design is i want to retrieve 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 of 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 suppose to be one result

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

i know that query is wrong but how to achieve that please help me out

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM