使用$和$ in从mongodb中检索结果

时间:2014-09-29 14:45:12

标签: java mongodb

我的架构设计是我想从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"
                }
        ]
}

我想获得2011年印度的数据

我使用了此查询

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

会返回两个结果

{
        "_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" : "23423q53q45345",       
        "value" : "5942178562002.65",   
        "dataset" : "GDP (current US$)",
        "data" : [
                { 
                        "data_name" : "country",
                        "value" : "india"
                },
                {
                        "data_name" : "date",
                        "value" : "2011"
                }
        ]
}

我知道查询错误,但如何实现这一点请帮助我

1 个答案:

答案 0 :(得分:1)

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