如何获取嵌套的mongodb文档的键

时间:2019-05-21 11:43:24

标签: python mongodb

我有这样的数据,我需要此mongodb集合中的所有键,例如property1,property2等。

{
    "_id": 0,
    "config": [{
            "property1":"a",
            "property2": "b",
            "property3": "c",
            "property4": "d"
        },
        {
            "property1": "a",
            "property2": "bb",
            "property3": "cc",
            "property4": "d",
            "ispropert5": true
        },
        {
            "property1": "a",
            "property2": "b",
            "property3": "c",
            "property4": "ddd",
            "ispropert5": false,
            "ispropert6": false
        }
    ],
    "entity": "123asdf",
    "url": "",
    "createdDate": 1
}

我尝试过

db.COLLECTION_NAME.find({},{config:1}).forEach(function(doc){Object.keys(doc).forEach(function(key){innerkeys[key]=1})}):

1 个答案:

答案 0 :(得分:0)

您可以使用$objectToArray动态读取对象键,然后将$reduce$concatArrays运行以合并结果:

conv2d

Mongo playground