How do I obtain the distribution of fields among a MongoDB collection, i.e the total count of documents for each field (without knowing the fields) ?
E.g. considering these documents :
{ "doc": { "a": …, "b": … } }
{ "doc": { "a": …, "c": … } }
{ "doc": { "a": …, "c": …, "d": { "e": … } } }
I would like to get
{ "a": 3, "b": 1, "c": 2, "d": 1, "d.e": 1 }
Studio3T has a "Schema" feature which does exactly that (and a bit more) for a random sample of the DB, how is the query constructed ?
One way is to use db.collection.countDocuments() with the $exists operator: