如何获得字段在MongoDB集合中的分布,即每个字段(不知道字段) 的文档总数?
例如,考虑这些文档 :
{ "doc": { "a": …, "b": … } }
{ "doc": { "a": …, "c": … } }
{ "doc": { "a": …, "c": …, "d": { "e": … } } }我想要一个
{ "a": 3, "b": 1, "c": 2, "d": 1, "d.e": 1 }Studio3T有一个“模式”特性,对于随机的DB样本,它确实可以做到这一点(甚至更多),查询是如何构造 的呢?
发布于 2021-01-29 05:25:27
一种方法是将db.collection.countDocuments()与$exists操作符一起使用:
db.collection.countDocuments({ a: { $exists: true});
db.collection.countDocuments({ b: { $exists: true});
db.collection.countDocuments({ c: { $exists: true});https://stackoverflow.com/questions/65944571
复制相似问题