找到子阵列的文件? [英] find the document from sub array?

查看:219
本文介绍了找到子阵列的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文档集合看起来像这样的:

I have a collection with documents that look like this:

"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."
}

将我们在找到()命令使用什么样的查询文档返回所有电影 my_collection 集合中无论是赢了还是被提名为最佳影片?

What query document would we use in a find() command to return all movies in the my_collection collection that either won or were nominated for best picture?

推荐答案

您需要使用点符号指定的嵌入文档的精确匹配,并使用 运营商$以选择的赢了还是被提名为最佳影片的那些文件

You need to use the dot notation to specify an exact match on the embedded document and use the $in operator to select those documents that won or were nominated for best picture

您不应该使用 $或这里运营商,因为它会执行一个集扫描,如果不是所有的条款是由指标作为<一提的支持href=\"https://docs.mongodb.org/manual/reference/operator/query/or/#or-clauses-and-indexes\">documentation.

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

这篇关于找到子阵列的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆