0
votes

Is there a query for returning all the fields (keys) that exist in a collection?

Due to the fact that MongoDB has a flexible document structure, the fields change from doc to doc in a collection, I would like to query for all the fields in a collection.

f.e. {"color":"red", "shape":"round", "radius":3}, {"color":"green", "shape":"square", "length":2, "width":3}

This docs are in the same collection. I would like the query to return - {"color", "shape", "radius", "length", "width"}

Thank you.

1

1 Answers

2
votes

Due to the fact that each document in a collection can have a different numbers of keys there is no good way of doing this, MongoDB holds no (unlike SQL) meta data about what keys exist within the collection.

There are however a few tools that be of help here:

And here is a related question with some answers: