你如何在mongodb中查询数组

时间:2017-05-30 13:52:51

标签: python mongodb

{
 "_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"
   }]

}

如果我只想在字段中显示值"方法"?如何查询此数组?

如何使用Pymongo解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用以下Mongo Shell查询

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

此查询将在您的示例文档中产生以下结果

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