Skip to content
Advertisement

How to get keys of nested mongodb documents

I have data like this, I need all the keys from this mongodb collection like property1,property2 etc.

JavaScript

I tried

JavaScript

Advertisement

Answer

You can use $objectToArray to read object keys dynamically and then run $reduce with $concatArrays to merge the results:

JavaScript

Output:

JavaScript

Mongo playground

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement