Although this question has been asked many times, it seems that none of the answers provides the desired result. I wonder if there is a way in MongoDB to create all keys/fields of all documents (including nested documents) of a collection.To illustrate the issue, here a short example:
Given the following collection:
[
{
"A": "B",
"C": {
"D": "E",
"F": "G"
}
},
{
"H": "I",
"J": {
"K": {
"L": "M",
"N": "O"
},
"P": "Q"
}
}
]
I want to get the following set of keys as output:
{
"keys": ["A", "C.D", "C.F", "H", "J.K.L", "J.K.N", "J.P"]
}
All other solutions I found so far only return the top-level keys:
{
"keys": ["A", "C", "H", "J"]
}
I've already experimented a lot with the aggregation pipeline, but for the life of me could not figure out how to convince it to give me the desired result. Maybe someone could help me out here.