从子数组中找到文档? [英] find the document from sub array?

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

问题描述

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

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

我们将在 find() 命令中使用什么查询文档来返回 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?

推荐答案

您需要使用 dot符号 指定嵌入文档的完全匹配并使用$in 操作符来选择那些获得或被提名最佳影片的文件

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

您不应该使用 $or 操作符在这里,因为如果索引不支持所有子句,它将执行集合扫描,如 文档.

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

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

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