根据标题,我想知道是否有办法获取 MongoDB 索引中的文档数。
说清楚,我是不是 寻找以下任一项:
.count()
. .stats()
. 最佳答案
索引引用其集合中的所有文档,除非索引是 sparse index或 partial index .从文档:
Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value. The index skips over any document that is missing the indexed field. The index is “sparse” because it does not include all documents of a collection. By contrast, non-sparse indexes contain all documents in a collection, storing null values for those documents that do not contain the indexed field.
Partial indexes only index the documents in a collection that meet a specified filter expression
所以 ...
db.collection.count()
db.collection.find().hint('index_name_here').count()
关于mongodb - 获取 MongoDB 索引中的文档数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326358/