MongoDB: List all possible keys in the collection

db.your_collection.aggregate([
  {"$project":{"arrayofkeyvalue":{"$objectToArray":"$ROOT"}}},
  {"$unwind":"$arrayofkeyvalue"},
  {"$group":{"_id":null,"allkeys":{"$addToSet":"$arrayofkeyvalue.k"}}}
])

Leave a Reply

Your email address will not be published. Required fields are marked *