从子数组中找到文档?

YAM*_*YAM 3 arrays mongodb mongodb-query

我有一个包含以下文档的集合:

"awards" : {
    "oscars" : [
        {"award": "bestAnimatedFeature", "result": "won"},
        {"award": "bestMusic", "result": "won"},
        {"award": "bestPicture", "result": "nominated"},
        {"award": "bestSoundEditing", "result": "nominated"},
        {"award": "bestScreenplay", "result": "nominated"}
    ],
    "wins" : 56,
    "nominations" : 86,
    "text" : "Won 2 Oscars. Another 56 wins and 86 nominations."
}
Run Code Online (Sandbox Code Playgroud)

我们在find()命令中使用什么查询文件来返回my_collection集合中所有获得或被提名以获得最佳图片的电影?

sty*_*ane 6

您需要使用点表示法来指定嵌入文档的完全匹配,并使用$in运算符选择那些赢得或被提名为最佳图片的文档

您不应该在$or此处使用运算符,因为如果索引不支持所有子句,它将执行集合扫描,如文档中所述.

db.my_collection.find({ 
    'awards.oscars.award': 'bestPicture', 
    'awards.oscars.result': { '$in': [ 'won', 'nominated' ] } 
} )
Run Code Online (Sandbox Code Playgroud)