Pymongo文档查询,用于转换为INI fi

2024-07-05 15:44:33 发布

您现在位置:Python中文网/ 问答频道 /正文

{
"name":"food",
"core":{
    "group":{
        "carbs":{
            "USA":{
                "breakfast":"potatoes",
                "dinner":"pasta"
            },
            "europe":{
                "breakfast":"something",
                "dinner":"something"
            }
        },
        "dessert":{
                            "USA":{
                                    "breakfast":"potatoes",
                                    "dinner":"pasta"
                            },
                            "europe":{
                                    "breakfast":"something",
                                    "dinner":"something"
                            }
        },
        "veggies":{
                            "USA":{
                                    "breakfast":"potatoes",
                                    "dinner":"pasta"
                            },
                            "europe":{
                                    "breakfast":"something",
                                    "dinner":"something"
                            }
                     }  
              }
        }
 }

我正试图找到一种方法,只返回核心群体(碳水化合物、甜点和蔬菜)

我有以下python代码,但它返回文档中的所有值,而我只对core.group感兴趣

data = collection.foodie.find({"name":"food"},{"name":False, '_id':False, "core.groups.carbs.europe":False, "core.groups.dessert.europe":False, "core.groups.veggies.europe":False

最终的结果是一个INI文件,其中组的类型作为USA的节名,如下所示,这就是为什么我正在寻找一种方法,只列出组,以便在我不知道组时动态查找存储的实际值(将它们硬编码为core.group.sarth.europe)

[carbs]
breakfast=potatoes
dinner=pasta

[dessert]
breakfast=potatoes
dinner=pasta

[veggies]
breakfast=potatoes
dinner=pasta

Tags: namecorefalsegroupsomethinggroupsdinnereurope