db.collection.stats()

    在本页面

    返回有关集合的统计信息。

    该方法包括以下参数:

    参数 类型 描述
    scale number 可选的。输出中使用的比例显示项目的大小。默认情况下,输出显示bytes中的大小。要显示千字节而不是字节,请指定1024 value 1024
    如果您指定非整数比例因子,则MongoDB将使用指定因子的整数部分。例如,如果将比例因子指定为1023.999,则MongoDB将使用1023该比例因子。
    从4.2版开始,输出包括scaleFactor 用于缩放大小值的输出。
    indexDetails boolean 可选的。如果为true,则db.collection.stats()除收集统计信息外,还返回 index details
    仅适用于WiredTiger存储引擎。
    默认为false
    indexDetailsKey document 可选的。如果indexDetailstrue,则可以使用indexDetailsKey通过指定索引 key 规范来过滤索引详细信息。只返回与indexDetailsKey完全匹配的索引。
    如果找不到匹配项,indexDetails将显示所有索引的统计信息。
    使用getIndexes()发现索引键。你不能将indexDetailsKeyindexDetailsName一起使用。
    indexDetailsName string 可选的。如果indexDetailstrue,则可以使用indexDetailsName通过指定索引name来过滤索引详细信息。只返回与indexDetailsName完全匹配的索引名称。
    如果找不到匹配项,indexDetails将显示所有索引的统计信息。
    使用getIndexes()来发现索引名称。你不能将indexDetailsNameindexDetailsField一起使用。

    仅指定scale因素,MongoDB支持旧格式:

    1. db.collection.stats(<number>)

    返回值: 包含有关指定集合的统计信息的文档。请参阅collStats以获取返回统计信息的细分。

    db.collection.stats()方法提供了围绕数据库命令的包装collStats

    行为

    缩放大小

    除非度量标准名称(例如"bytes currently in the cache" )另外指定,与大小相关的值以字节为单位显示,可以按比例覆盖。

    比例因子将受影响的大小值四舍五入为整数。

    存储引擎

    根据存储引擎,返回的数据可能不同。有关字段的详细信息,请参阅输出详细信息。

    意外关机后的准确性

    使用Wired Tiger存储引擎不正常关闭mongod后,db.collection.stats()报告的计数和大小统计信息可能不准确。

    偏移量取决于在最后检查站和不干净关闭之间执行的 insert,update 或 delete 操作的数量。检查点通常每 60 秒发生一次。但是,使用 non-default —syncdelay设置运行mongod实例可能会有更多或更少的检查点。

    在mongod上的每个集合上运行验证以在不正常关闭后恢复正确的统计信息。

    索引过滤器行为

    使用indexDetailsKeyindexDetailsName过滤indexDetails将仅_return 单个匹配的索引。如果未找到确切的 match,indexDetails将显示有关集合的所有索引的信息。

    indexDetailsKey字段采用以下形式的文档:

    1. { '<string>' : <value>, '<string>' : <value>, ... }

    其中<string>是索引的字段,<value>是索引的方向,或特殊索引类型,如text2dsphere。有关索引类型的完整列表,请参见索引类型。

    意外停机和计数

    对于使用WiredTiger存储引擎的 MongoDB 实例,在不正常关闭后,大小和计数的统计信息可能会被collStats,dbStats,计数报告最多 1000 个文档。要恢复集合的正确统计信息,请在集合上运行 run 验证。

    进行中索引

    从MongoDB 4.2开始,db.collection.stats包括有关当前正在构建的索引的信息。有关详细信息,请参见:

    • collStats.nindexes
    • collStats.indexDetails
    • collStats.indexBuilds
    • collStats.totalIndexSize
    • collStats.indexSizes

      例子

    注意

    您可以在入门指南中找到用于这些示例的集合数据

    基本统计查询

    以下操作返回restaurants集合上的统计信息:

    1. db.restaurants.stats()

    操作返回:

    1. {
    2. "ns" : "guidebook.restaurants",
    3. "count" : 25359,
    4. "size" : 10630398,
    5. "avgObjSize" : 419,
    6. "storageSize" : 4104192
    7. "capped" : false,
    8. "wiredTiger" : {
    9. "metadata" : {
    10. "formatVersion" : 1
    11. },
    12. "creationString" : "allocation_size=4KB,app_metadata=(formatVersion=1),block_allocation=best,block_compressor=snappy,cache_resident=0,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=0,extractor=,format=btree,huffman_key=,huffman_value=,immutable=0,internal_item_max=0,internal_key_max=0,internal_key_truncate=,internal_page_max=4KB,key_format=q,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=64MB,log=(enabled=),lsm=(auto_throttle=,bloom=,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=0,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=10m,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=0,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,type=file,value_format=u",
    13. "type" : "file",
    14. "uri" : "statistics:table:collection-2-7253336746667145592",
    15. "LSM" : {
    16. "bloom filters in the LSM tree" : 0,
    17. "bloom filter false positives" : 0,
    18. "bloom filter hits" : 0,
    19. "bloom filter misses" : 0,
    20. "bloom filter pages evicted from cache" : 0,
    21. "bloom filter pages read into cache" : 0,
    22. "total size of bloom filters" : 0,
    23. "sleep for LSM checkpoint throttle" : 0,
    24. "chunks in the LSM tree" : 0,
    25. "highest merge generation in the LSM tree" : 0,
    26. "queries that could have benefited from a Bloom filter that did not exist" : 0,
    27. "sleep for LSM merge throttle" : 0
    28. },
    29. "block-manager" : {
    30. "file allocation unit size" : 4096,
    31. "blocks allocated" : 338,
    32. "checkpoint size" : 4096000,
    33. "allocations requiring file extension" : 338,
    34. "blocks freed" : 0,
    35. "file magic number" : 120897,
    36. "file major version number" : 1,
    37. "minor version number" : 0,
    38. "file bytes available for reuse" : 0,
    39. "file size in bytes" : 4104192
    40. },
    41. "btree" : {
    42. "btree checkpoint generation" : 15,
    43. "column-store variable-size deleted values" : 0,
    44. "column-store fixed-size leaf pages" : 0,
    45. "column-store internal pages" : 0,
    46. "column-store variable-size leaf pages" : 0,
    47. "pages rewritten by compaction" : 0,
    48. "number of key/value pairs" : 0,
    49. "fixed-record size" : 0,
    50. "maximum tree depth" : 3,
    51. "maximum internal page key size" : 368,
    52. "maximum internal page size" : 4096,
    53. "maximum leaf page key size" : 3276,
    54. "maximum leaf page size" : 32768,
    55. "maximum leaf page value size" : 67108864,
    56. "overflow pages" : 0,
    57. "row-store internal pages" : 0,
    58. "row-store leaf pages" : 0
    59. },
    60. "cache" : {
    61. "bytes read into cache" : 9309503,
    62. "bytes written from cache" : 10817368,
    63. "checkpoint blocked page eviction" : 0,
    64. "unmodified pages evicted" : 0,
    65. "page split during eviction deepened the tree" : 0,
    66. "modified pages evicted" : 1,
    67. "data source pages selected for eviction unable to be evicted" : 0,
    68. "hazard pointer blocked page eviction" : 0,
    69. "internal pages evicted" : 0,
    70. "pages split during eviction" : 1,
    71. "in-memory page splits" : 1,
    72. "overflow values cached in memory" : 0,
    73. "pages read into cache" : 287,
    74. "overflow pages read into cache" : 0,
    75. "pages written from cache" : 337
    76. },
    77. "compression" : {
    78. "raw compression call failed, no additional data available" : 0,
    79. "raw compression call failed, additional data available" : 0,
    80. "raw compression call succeeded" : 0,
    81. "compressed pages read" : 287,
    82. "compressed pages written" : 333,
    83. "page written failed to compress" : 0,
    84. "page written was too small to compress" : 4
    85. },
    86. "cursor" : {
    87. "create calls" : 1,
    88. "insert calls" : 25359,
    89. "bulk-loaded cursor-insert calls" : 0,
    90. "cursor-insert key and value bytes inserted" : 10697901,
    91. "next calls" : 76085,
    92. "prev calls" : 1,
    93. "remove calls" : 0,
    94. "cursor-remove key bytes removed" : 0,
    95. "reset calls" : 25959,
    96. "restarted searches" : 0,
    97. "search calls" : 0,
    98. "search near calls" : 594,
    99. "update calls" : 0,
    100. "cursor-update value bytes updated" : 0
    101. },
    102. "reconciliation" : {
    103. "dictionary matches" : 0,
    104. "internal page multi-block writes" : 1,
    105. "leaf page multi-block writes" : 2,
    106. "maximum blocks required for a page" : 47,
    107. "internal-page overflow keys" : 0,
    108. "leaf-page overflow keys" : 0,
    109. "overflow values written" : 0,
    110. "pages deleted" : 0,
    111. "page checksum matches" : 0,
    112. "page reconciliation calls" : 4,
    113. "page reconciliation calls for eviction" : 1,
    114. "leaf page key bytes discarded using prefix compression" : 0,
    115. "internal page key bytes discarded using suffix compression" : 333
    116. },
    117. "session" : {
    118. "object compaction" : 0,
    119. "open cursor count" : 1
    120. },
    121. "transaction" : {
    122. "update conflicts" : 0
    123. }
    124. },
    125. "nindexes" : 4,
    126. "totalIndexSize" : 626688,
    127. "indexSizes" : {
    128. "_id_" : 217088,
    129. "borough_1_cuisine_1" : 139264,
    130. "cuisine_1" : 131072,
    131. "borough_1_address.zipcode_1" : 139264
    132. },
    133. "ok" : 1
    134. }

    由于统计数据未给出比例参数,因此所有大小值都在bytes中。

    带有比例的统计查询

    以下操作通过指定scalescale来更改从byteskilobytes的数据比例:

    1. db.restaurants.stats( { scale : 1024 } )

    操作返回:

    1. {
    2. "ns" : "guidebook.restaurants",
    3. "count" : 25359,
    4. "size" : 10381,
    5. "avgObjSize" : 419,
    6. "storageSize" : 4008,
    7. "capped" : false,
    8. "wiredTiger" : {
    9. ...
    10. },
    11. "nindexes" : 4,
    12. "totalIndexSize" : 612,
    13. "indexSizes" : {
    14. "_id_" : 212,
    15. "borough_1_cuisine_1" : 136,
    16. "cuisine_1" : 128,
    17. "borough_1_address.zipcode_1" : 136
    18. },
    19. "ok" : 1
    20. }

    带索引详细信息的统计查找

    以下操作将创建一个indexDetails文档,其中包含与集合中每个索引相关的信息:

    1. db.restaurant.stats( { indexDetails : true } )

    操作返回:

    1. {
    2. "ns" : "guidebook.restaurants",
    3. "count" : 25359,
    4. "size" : 10630398,
    5. "avgObjSize" : 419,
    6. "storageSize" : 4104192,
    7. "capped" : false,
    8. "wiredTiger" : {
    9. ...
    10. },
    11. "nindexes" : 4,
    12. "indexDetails" : {
    13. "_id_" : {
    14. "metadata" : {
    15. "formatVersion" : 6,
    16. "infoObj" : "{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "blogs.posts" }"
    17. },
    18. "creationString" : "allocation_size=4KB,app_metadata=(formatVersion=6,infoObj={ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "blogs.posts" }),block_allocation=best,block_compressor=,cache_resident=0,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=0,extractor=,format=btree,huffman_key=,huffman_value=,immutable=0,internal_item_max=0,internal_key_max=0,internal_key_truncate=,internal_page_max=16k,key_format=u,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=16k,leaf_value_max=0,log=(enabled=),lsm=(auto_throttle=,bloom=,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=0,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=5MB,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=true,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=75,type=file,value_format=u",
    19. "type" : "file",
    20. "uri" : "statistics:table:index-3-7253336746667145592",
    21. "LSM" : {
    22. ...
    23. },
    24. "block-manager" : {
    25. ...
    26. },
    27. "btree" : {
    28. ...
    29. },
    30. "cache" : {
    31. ...
    32. },
    33. "compression" : {
    34. ...
    35. },
    36. "cursor" : {
    37. ...
    38. },
    39. "reconciliation" : {
    40. ...
    41. },
    42. "session" : {
    43. ...
    44. },
    45. "transaction" : {
    46. ...
    47. }
    48. },
    49. "borough_1_cuisine_1" : {
    50. "metadata" : {
    51. "formatVersion" : 6,
    52. "infoObj" : "{ "v" : 1, "key" : { "borough" : 1, "cuisine" : 1 }, "name" : "borough_1_cuisine_1", "ns" : "blogs.posts" }"
    53. },
    54. "creationString" : "allocation_size=4KB,app_metadata=(formatVersion=6,infoObj={ "v" : 1, "key" : { "borough" : 1, "cuisine" : 1 }, "name" : "borough_1_cuisine_1", "ns" : "blogs.posts" }),block_allocation=best,block_compressor=,cache_resident=0,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=0,extractor=,format=btree,huffman_key=,huffman_value=,immutable=0,internal_item_max=0,internal_key_max=0,internal_key_truncate=,internal_page_max=16k,key_format=u,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=16k,leaf_value_max=0,log=(enabled=),lsm=(auto_throttle=,bloom=,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=0,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=5MB,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=true,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=75,type=file,value_format=u",
    55. "type" : "file",
    56. "uri" : "statistics:table:index-4-7253336746667145592",
    57. "LSM" : {
    58. ...
    59. },
    60. "block-manager" : {
    61. ...
    62. },
    63. "btree" : {
    64. ...
    65. },
    66. "cache" : {
    67. ...
    68. },
    69. "compression" : {
    70. ...
    71. },
    72. "cursor" : {
    73. ...
    74. },
    75. "reconciliation" : {
    76. ...
    77. },
    78. "session" : {
    79. "object compaction" : 0,
    80. "open cursor count" : 0
    81. },
    82. "transaction" : {
    83. "update conflicts" : 0
    84. }
    85. },
    86. "cuisine_1" : {
    87. "metadata" : {
    88. "formatVersion" : 6,
    89. "infoObj" : "{ "v" : 1, "key" : { "cuisine" : 1 }, "name" : "cuisine_1", "ns" : "blogs.posts" }"
    90. },
    91. "creationString" : "allocation_size=4KB,app_metadata=(formatVersion=6,infoObj={ "v" : 1, "key" : { "cuisine" : 1 }, "name" : "cuisine_1", "ns" : "blogs.posts" }),block_allocation=best,block_compressor=,cache_resident=0,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=0,extractor=,format=btree,huffman_key=,huffman_value=,immutable=0,internal_item_max=0,internal_key_max=0,internal_key_truncate=,internal_page_max=16k,key_format=u,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=16k,leaf_value_max=0,log=(enabled=),lsm=(auto_throttle=,bloom=,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=0,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=5MB,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=true,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=75,type=file,value_format=u",
    92. "type" : "file",
    93. "uri" : "statistics:table:index-5-7253336746667145592",
    94. "LSM" : {
    95. ...
    96. },
    97. "block-manager" : {
    98. ...
    99. },
    100. "btree" : {
    101. ...
    102. },
    103. "cache" : {
    104. ...
    105. },
    106. "compression" : {
    107. ...
    108. },
    109. "cursor" : {
    110. ...
    111. },
    112. "reconciliation" : {
    113. ...
    114. },
    115. "session" : {
    116. ...
    117. },
    118. "transaction" : {
    119. ...
    120. }
    121. },
    122. "borough_1_address.zipcode_1" : {
    123. "metadata" : {
    124. "formatVersion" : 6,
    125. "infoObj" : "{ "v" : 1, "key" : { "borough" : 1, "address.zipcode" : 1 }, "name" : "borough_1_address.zipcode_1", "ns" : "blogs.posts" }"
    126. },
    127. "creationString" : "allocation_size=4KB,app_metadata=(formatVersion=6,infoObj={ "v" : 1, "key" : { "borough" : 1, "address.zipcode" : 1 }, "name" : "borough_1_address.zipcode_1", "ns" : "blogs.posts" }),block_allocation=best,block_compressor=,cache_resident=0,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=0,extractor=,format=btree,huffman_key=,huffman_value=,immutable=0,internal_item_max=0,internal_key_max=0,internal_key_truncate=,internal_page_max=16k,key_format=u,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=16k,leaf_value_max=0,log=(enabled=),lsm=(auto_throttle=,bloom=,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=0,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=5MB,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=true,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=75,type=file,value_format=u",
    128. "type" : "file",
    129. "uri" : "statistics:table:index-6-7253336746667145592",
    130. "LSM" : {
    131. ...
    132. },
    133. "block-manager" : {
    134. ...
    135. },
    136. "btree" : {
    137. ...
    138. },
    139. "cache" : {
    140. ...
    141. },
    142. "compression" : {
    143. ...
    144. },
    145. "cursor" : {
    146. ...
    147. },
    148. "reconciliation" : {
    149. ...
    150. },
    151. "session" : {
    152. ...
    153. },
    154. "transaction" : {
    155. ...
    156. }
    157. }
    158. },
    159. "totalIndexSize" : 626688,
    160. "indexSizes" : {
    161. "_id_" : 217088,
    162. "borough_1_cuisine_1" : 139264,
    163. "cuisine_1" : 131072,
    164. "borough_1_address.zipcode_1" : 139264
    165. },
    166. "ok" : 1
    167. }

    带有过滤索引详细信息的统计信息查找

    要过滤indexDetails字段中的索引,可以使用indexDetailsKey选项指定索引键,也可以使用indexDetailsName指定索引 name。要发现集合的索引键和名称,请使用db.collection.getIndexes()。

    给定以下索引:

    1. {
    2. "ns" : "guidebook.restaurants",
    3. "v" : 1,
    4. "key" : {
    5. "borough" : 1,
    6. "cuisine" : 1
    7. },
    8. "name" : "borough_1_cuisine_1"
    9. }

    以下操作将indexDetails文档过滤为indexDetailsKey文档定义的单个索引。

    1. db.restaurants.stats(
    2. {
    3. 'indexDetails' : true,
    4. 'indexDetailsKey' :
    5. {
    6. 'borough' : 1,
    7. 'cuisine' : 1
    8. }
    9. }
    10. )

    以下操作将indexDetails文档过滤为indexDetailsName文档定义的单个索引。

    1. db.restaurants.stats(
    2. {
    3. 'indexDetails' : true,
    4. 'indexDetailsName' : 'borough_1_cuisine_1'
    5. }
    6. )

    两个操作都会 return 相同的输出:

    1. {
    2. "ns" : "blogs.restaurants",
    3. "count" : 25359,
    4. "size" : 10630398,
    5. "avgObjSize" : 419,
    6. "storageSize" : 4104192,
    7. "capped" : false,
    8. "wiredTiger" : {
    9. ...
    10. },
    11. "nindexes" : 4,
    12. "indexDetails" : {
    13. "borough_1_cuisine_1" : {
    14. "metadata" : {
    15. "formatVersion" : 6,
    16. "infoObj" : "{ "v" : 1, "key" : { "borough" : 1, "cuisine" : 1 }, "name" : "borough_1_cuisine_1", "ns" : "blogs.posts" }"
    17. },
    18. "creationString" : "allocation_size=4KB,app_metadata=(formatVersion=6,infoObj={ "v" : 1, "key" : { "borough" : 1, "cuisine" : 1 }, "name" : "borough_1_cuisine_1", "ns" : "blogs.posts" }),block_allocation=best,block_compressor=,cache_resident=0,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=0,extractor=,format=btree,huffman_key=,huffman_value=,immutable=0,internal_item_max=0,internal_key_max=0,internal_key_truncate=,internal_page_max=16k,key_format=u,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=16k,leaf_value_max=0,log=(enabled=),lsm=(auto_throttle=,bloom=,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=0,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=5MB,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=true,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=75,type=file,value_format=u",
    19. "type" : "file",
    20. "uri" : "statistics:table:index-4-7253336746667145592",
    21. "LSM" : {
    22. ...
    23. },
    24. "block-manager" : {
    25. ...
    26. },
    27. "btree" : {
    28. ...
    29. },
    30. "cache" : {
    31. ...
    32. },
    33. "compression" : {
    34. ...
    35. },
    36. "cursor" : {
    37. ...
    38. },
    39. "reconciliation" : {
    40. ...
    41. },
    42. "session" : {
    43. ...
    44. },
    45. "transaction" : {
    46. ...
    47. }
    48. }
    49. },
    50. "totalIndexSize" : 626688,
    51. "indexSizes" : {
    52. "_id_" : 217088,
    53. "borough_1_cuisine_1" : 139264,
    54. "cuisine_1" : 131072,
    55. "borough_1_address.zipcode_1" : 139264
    56. },
    57. "ok" : 1
    58. }

    有关输出的说明,请参阅输出细节。

    也可以看看

    $collStats

    译者:李冠飞

    校对:

    参见

    原文 - db.collection.stats()