2017-05-30 145 views
0
{ 
"_id":123456, 
"Menu":[{ 
    "Dish":"Apple pie", 
    "Rating": "Good", 
    "Method": "Oven Baked" 
    }, 

    { 
    "Dish":"Pumpkin Pie", 
    "Rating": "Bad", 
    "Method": "Baked" 
    }, 
    { 
    "Dish":"Tomato Soup", 
    "Rating": "Good", 
    "Method": "Boiled" 
    }] 

}你如何查詢MongoDB中的數組

如何查詢這個數組,如果我只想顯示領域的「法」的價值觀?

如何用Pymongo解決這個問題?

+1

您可以使用您的查詢'projection':HTTP:// API .mongodb.com /蟒/電流/ API/pymongo/collection.html?突出=投影#pymongo.collection.Collection.find – errata

回答

0

使用下面蒙戈殼牌查詢

db.mycollection.find({_id:123456}, {"Menu.Method":1}) 

此查詢將產生對示例文檔如下的結果

{ 
     "_id" : 123456, 
     "Menu" : [ 
       { 
         "Method" : "Oven Baked" 
       }, 
       { 
         "Method" : "Baked" 
       }, 
       { 
         "Method" : "Boiled" 
       } 
     ] 
}