1

我目前有以下数据集(简化):

{  
    'component_id':1,  
    '_locales':[   
        {  
            'url': 'dutch',  
            'locale': 'nl_NL'  
        },  
        {  
            'url': 'english',  
            'locale': 'en_US'  
        }  
    ]  
} (etc more rows similar to this but unique urls)  

当我查询特定的 url 和语言环境时,我使用以下查询

db.find({'_locales': { '$elemMatch': { 'locale': 'nl_NL', 'url': 'dutch' } }});

我得到了正确的行但是'_locales'返回了整个数组,包括我不需要的en_US,无论如何它只返回匹配的数组行,在这种情况下:

'_locales':[   
{  
     'url': 'dutch',  
     'locale': 'nl_NL'  
}]  

我有一种感觉,我必须遍历语言环境并将行与语言环境匹配。感觉不对,是否有更好的解决方案来做到这一点(不迭代结果集)?例如,改变表结构?我希望这样做而不是只为语言环境制作第二张桌子..

4

1 回答 1

4

You always query for top level documents. Just because your query criteria involve matching a specific array element doesn't tell MongoDB that it should only return that element. Currently there's no way to return specific array elements other than using the $slice operator which is not what you need here.

There are feature requests in MongoDB JIRA that will allow for what you want but currently it is not possible.

于 2012-04-20T11:10:26.113 回答