MongoDB如何通过匹配多个对象值来查询嵌套对象数组?

时间:2019-01-13 13:06:35

标签: arrays mongodb mongodb-query

我有许多通过以下方式构造的数据记录:

example record 1
{
    "_id" : ObjectId("5c3aa368dd1460b0bc434156"),
    "id" : "XXWE124124123",
    "accountId" : "XXX2311ewqee",
    "name" : "BOB",
    "region" : "euw",
    "champions" : [ 
        {
            "_id" : ObjectId("5c3aa368dd1460b0bc4341ba"),
            "id" : 412,
            "rank" : "DIAMOND",
            "kills" : "5",
            "assists" : "18",
            "deaths" : "7",
            "wins" : true,
            "lane" : "BOTTOM",
            "spells" : "4-14",
            "trinket" : "3364",
            "items" : "items-3117-3109-3069-2055-3190-3801-3364",
            "runes" : "runes-8400-8300-8439-8463-8429-8451-8345-8347-5007-5002-5002"
        }, 
        {
            "_id" : ObjectId("5c3aa368dd1460b0bc4341b9"),
            "id" : 12,
            "rank" : "DIAMOND",
            "kills" : "0",
            "assists" : "4",
            "deaths" : "8",
            "wins" : false,
            "lane" : "BOTTOM",
            "spells" : "4-14",
            "trinket" : "3364",
            "items" : "items-3117-3050-3069-2055-3067-1031-3364",
            "runes" : "runes-8400-8300-8439-8463-8429-8451-8345-8347-5007-5003-5002"
        }, 
        {
            "_id" : ObjectId("5c3aa368dd1460b0bc4341b8"),
            "id" : 412,
            "rank" : "DIAMOND",
            "kills" : "2",
            "assists" : "15",
            "deaths" : "7",
            "wins" : false,
            "lane" : "BOTTOM",
            "spells" : "4-14",
            "trinket" : "3364",
            "items" : "items-3117-3050-3069-2055-3105-3109-3364",
            "runes" : "runes-8400-8300-8439-8463-8429-8451-8345-8347-5007-5002-5002"
        }, 
        ...
example record 2
{
    "_id" : ObjectId("8888a368dd1460b0bc434156"),
    "id" : "YYWE124124123",
    "accountId" : "YYY2311ewqee",
    "name" : "JOE",
    "region" : "euw",
    "champions" : [ 
        {
            "_id" : ObjectId("5c3aa368dd1460b0bc4342cb"),
            "id" : 112,
            "rank" : "GOLD",
            "kills" : "5",
            "assists" : "18",
            "deaths" : "7",
            "wins" : true,
            "lane" : "BOTTOM",
            "spells" : "4-14",
            "trinket" : "3364",
            "items" : "items-3117-3109-3069-2055-3190-3801-3364",
            "runes" : "runes-8400-8300-8439-8463-8429-8451-8345-8347-5007-5002-5002"
        }, 
        {
            "_id" : ObjectId("5c3aa368dd1460b0bc434cs9"),
            "id" : 412,
            "rank" : "DIAMOND",
            "kills" : "0",
            "assists" : "4",
            "deaths" : "8",
            "wins" : false,
            "lane" : "BOTTOM",
            "spells" : "4-14",
            "trinket" : "3364",
            "items" : "items-3117-3050-3069-2055-3067-1031-3364",
            "runes" : "runes-8400-8300-8439-8463-8429-8451-8345-8347-5007-5003-5002"
        }, 
        {
            "_id" : ObjectId("5c3aa368dd1460b0bc434666"),
            "id" : 412,
            "rank" : "GOLD",
            "kills" : "2",
            "assists" : "15",
            "deaths" : "7",
            "wins" : false,
            "lane" : "BOTTOM",
            "spells" : "4-14",
            "trinket" : "3364",
            "items" : "items-3117-3050-3069-2055-3105-3109-3364",
            "runes" : "runes-8400-8300-8439-8463-8429-8451-8345-8347-5007-5002-5002"
        }, 
        ...
example record 3..
example record 4..
etc

我想通过Champions对象数组中的多个对象值进行查询。例如,我想返回所有同时包含Champions.id:412和Champions.rank:'DIAMOND'的数组。

我已经尝试使用以下两个查询:

ChampionData.find({ champions: {$elemMatch: {id:412,rank:'DIAMOND'} } }

我也尝试过

ChampionData.find({$and:[{"champions.id": id},{"champions.rank":rank}]}

但是,这似乎返回了整个示例记录,其中的一些Champion.id不是412。

1 个答案:

答案 0 :(得分:0)

您的查询是正确的,但是您发现它返回符合find条件的整个文档,而不仅仅是返回您需要的嵌套文档。

为了只取回匹配的嵌套文档,您必须使用MongoDB的$ positional运算符仅“投影”出匹配的项目。因此,您可以这样做:

ChampionData.find({ champions: {$elemMatch: {id:412,rank:'DIAMOND'} } },
   { 'champions.$': 1 }
)