find the document from sub array?

后端 未结 2 1126
悲&欢浪女
悲&欢浪女 2020-12-21 06:15

I have a collection with documents that look like this:

\"awards\" : {
    \"oscars\" : [
        {\"award\": \"bestAnimatedFeature\", \"result\": \"won\"},
         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-21 06:15

    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

    You shouldn't use the $or operator here because it will perform a collection scan if not all the clauses are supported by indexes as mention in the documentation.

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

提交回复
热议问题