mongodb - 获取 MongoDB 索引中的文档数

标签 mongodb indexing

根据标题,我想知道是否有办法获取 MongoDB 索引中的文档数。

说清楚,我是不是 寻找以下任一项:

  • 如何获取集合中的文档数量--.count() .
  • 如何获取索引的大小 -- .stats() .
  • 最佳答案

    索引引用其集合中的所有文档,除非索引是 sparse indexpartial 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/

    相关文章:

    python - 根据另一个 DataFrame 的公共(public)列过滤和替换一个 Pandas DataFrame 中的值

    mysql - 通过 mysql 中的临时表可以加快删除速度

    python - 在 Elasticsearch 中的所有字段上创建已分析和未分析索引

    linux - 重启mongo进程时mongo服务器问题

    java - mongodb java 驱动程序。如何通过引用返回对象

    MongoDB、MapReduce 和排序

    Mysql:前缀索引与索引

    matlab - 用于按组快速求和的矩阵索引

    MongoDB:按现有字段排序,然后按字母顺序排序

    database - MongoDB 无法识别的选项 --install