获取 MongoDB 中每个字段的文档数,例如 Studio3T Schema 操作

如何获取 MongoDB 集合中字段的分布,即每个字段的文档总数(不知道字段)?

例如考虑这些文件:

{ "doc": { "a": …,"b": … } }
{ "doc": { "a": …,"c": … } }
{ "doc": { "a": …,"c": …,"d": { "e": … } } }

我想得到

{ "a": 3,"b": 1,"c": 2,"d": 1,"d.e": 1 }

Studio3T 有一个“模式”功能,它对数据库的随机样本完全这样做(还有更多),查询是如何构造的?

anyellow 回答:获取 MongoDB 中每个字段的文档数,例如 Studio3T Schema 操作

一种方法是将 db.collection.countDocuments()$exists 运算符一起使用:

db.collection.countDocuments({ a: { $exists: true});
db.collection.countDocuments({ b: { $exists: true});
db.collection.countDocuments({ c: { $exists: true});
本文链接:https://www.f2er.com/1021432.html

大家都在问