MongoDB とドライバー
このページでは、 mongosh メソッドについて説明します。MongoDB ドライバーで同等のメソッドを確認するには、ご使用のプログラミング言語の対応するページを参照してください。
定義
db.collection.stats(<option>)重要
mongosh メソッド
このページでは、
mongoshメソッドについて記載しています。ただし、データベースコマンドや Node.js などの言語固有のドライバーのドキュメントには該当しません。データベースコマンドについては、
collStatsコマンドを参照してください。MongoDB API ドライバーについては、各言語の MongoDB ドライバー ドキュメントを参照してください。
コレクションに関する統計情報を返します。
メソッドの形式は次のとおりです。
db.collection.stats({ scale: <num>, // Optional indexDetails: <boolean>, // Optional indexDetailsKey: <document>, // Optional indexDetailsName: <string>. // Optional }) フィールドタイプ説明scale数値
任意。 さまざまなサイズ データの増やす ファクター。
scaleのデフォルトは 1 で、サイズ データをバイト単位で返します。 バイトではなくキロバイトを表示するには、scaleの値を1024に指定します。整数以外のスケール係数を指定した場合、MongoDB は指定された係数の整数部分を使用します。たとえば、スケール係数を
1023.999に指定すると、MongoDB はスケール係数として1023使用します。バージョン 4.2 以降では、出力にサイズ値を増やすのに使用される
scaleFactorが含まれるようになりました。indexDetailsブール値
任意。
trueの場合、 はコレクション統計に加えてdb.collection.stats()index detailsを返します。WiredTiger storage engine でのみ動作します。
デフォルトは
falseです。indexDetailsKeyドキュメント
任意。
indexDetailsがtrueの場合、インデックス キー仕様を指定することでindexDetailsKeyを使用してインデックスの詳細をフィルタリングできます。indexDetailsKeyと完全に一致するインデックスだけが返されます。一致するものが見つからない場合、
indexDetailsはすべてのインデックスの統計情報を表示します。getIndexes()を使用してインデックスキーを検索します。indexDetailsKeyはindexDetailsNameでは使用できません。indexDetailsNamestring
任意。
indexDetailsがtrueの場合、インデックスnameを指定することでindexDetailsNameを使用してインデックスの詳細をフィルタリングできます。indexDetailsNameと完全に一致するインデックス名だけが返されます。一致するものが見つからない場合、
indexDetailsはすべてのインデックスの統計情報を表示します。getIndexes()を使用してインデックス名を検索します。indexDetailsNameはindexDetailsFieldでは使用できません。scale係数のみを指定するために、MongoDB は次の従来の形式をサポートしています。db.collection.stats(<number>) 次の値を返します。 指定したコレクションの統計を含む ドキュメント です。返される統計の内訳については、 collStatsを参照してください。
注意
db.collection.stats()は、デフォルトで$collStats 集計ステージを内部的に呼び出します。
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
注意
このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
動作
スケーリングされたサイズ
メトリクス名("bytes
currently in the cache"など)が特に指定されていない限り、サイズに関連する値はバイト単位で表示され、 scale を使用して上書きできます。
スケール係数は、影響を受けるサイズの値を整数に丸めます。
ストレージ エンジン
storage engine によっては、返されるデータが異なる場合があります。フィールドの詳細については、出力の詳細を参照してください。
予期しないシャットダウン後の精度
Wired Tiger ストレージ エンジンを使用して mongod を不正にシャットダウンした後、db.collection.stats() によって報告されるカウントとサイズの統計が不正確になる可能性があります。
ドリフトの量は、チェックポイントからクリーン シャットダウンまでの間に実行された挿入、アップデート、または削除操作の数によって異なります。チェックポイントは通常、60 秒ごとに発現します。ただし、デフォルト以外の --syncdelay 設定で実行されている mongod インスタンスでは、チェックポイントの頻度が増減する可能性があります。
不正なシャットダウン後に統計を復元するには、mongod の各コレクションに対して validate を実行します。
不正なシャットダウン後:
レプリカセット ノードの状態の制限
レプリカセット ノードでcollStats操作を実行するには、ノードがPRIMARYまたはSECONDARY状態である必要があります。 ノードがSTARTUP2などの別の状態にある場合、操作はエラーになります。
インデックス フィルターの動作
indexDetailsKey または indexDetailsName を使用して indexDetails をフィルタリングすると、一致するインデックスが 1 つだけ返されます。完全に一致するものが見つからない場合、 indexDetails はコレクションのすべてのインデックスに関する情報を表示します。
indexDetailsKey フィールドは、次の形式のドキュメントを受け取ります。
{ '<string>' : <value>, '<string>' : <value>, ... }
<string>> はインデックスが作成されるフィールドで、<value> はインデックスの方向、または text や 2dsphere などの特殊なインデックスタイプです。インデックス タイプの全リストについては、「インデックス タイプ」を参照してください。
MongoDB 8.0以降では、インデックス フィルターを追加する 代わりに、 クエリ設定を使用します 。 インデックス フィルターは MongoDB 8.0以降非推奨です。
クエリ設定は、インデックス フィルターよりも多くの機能を持ちます。 また、インデックス フィルターは永続的ではなく、すべてのクラスター ノードに対してインデックス フィルターを簡単に作成することはできません。 クエリ設定を追加して例を探すには、 setQuerySettingsを参照してください。
予期しないシャットダウンとカウント
WiredTiger Storage Engineを使用する MongoDB インスタンスでは、シャットダウンが正常に行われないと、collStats 、dbStats および count で報告されているように、サイズとカウントの統計が最大 1000 ドキュメント分ずれることがあります。コレクションに関する正しい統計を復元するには、コレクションに対して validate を実行します。
進行中のインデックス
db.collection.stats() には、現在構築中のインデックスに関する情報が含まれています。詳細については、以下を参照してください。
例
注意
これらの例に使用されているコレクションデータは、 primer-dataset.json にあります。
基本的な統計の検索
次の操作は、test データベース内の restaurants コレクションに関する統計を返します。
db.restaurants.stats()
この操作では以下が返されます。
{ "ns" : "test.restaurants", "size" : 7557340, "count" : 25359, "avgObjSize" : 298, "storageSize" : 9670656, "freeStorageSize" : 6660096, "capped" : false, "wiredTiger" : { "metadata" : { "formatVersion" : 1 }, "creationString" : "access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=1),assert=(commit_timestamp=none,durable_timestamp=none,read_timestamp=none),block_allocation=best,block_compressor=snappy,cache_resident=false,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=false,extractor=,format=btree,huffman_key=,huffman_value=,ignore_in_memory_cache_size=false,immutable=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,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=true),lsm=(auto_throttle=true,bloom=true,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=false,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_custom=(prefix=,start_generation=0,suffix=),merge_max=15,merge_min=0),memory_page_image_max=0,memory_page_max=10m,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,type=file,value_format=u", "type" : "file", "uri" : "statistics:table:collection-43-6797839137631957852", "LSM" : { "bloom filter false positives" : 0, "bloom filter hits" : 0, "bloom filter misses" : 0, "bloom filter pages evicted from cache" : 0, "bloom filter pages read into cache" : 0, "bloom filters in the LSM tree" : 0, "chunks in the LSM tree" : 0, "highest merge generation in the LSM tree" : 0, "queries that could have benefited from a Bloom filter that did not exist" : 0, "sleep for LSM checkpoint throttle" : 0, "sleep for LSM merge throttle" : 0, "total size of bloom filters" : 0 }, "block-manager" : { "allocations requiring file extension" : 460, "blocks allocated" : 556, "blocks freed" : 420, "checkpoint size" : 2994176, "file allocation unit size" : 4096, "file bytes available for reuse" : 6660096, "file magic number" : 120897, "file major version number" : 1, "file size in bytes" : 9670656, "minor version number" : 0 }, "btree" : { "btree checkpoint generation" : 58, "column-store fixed-size leaf pages" : 0, "column-store internal pages" : 0, "column-store variable-size RLE encoded values" : 0, "column-store variable-size deleted values" : 0, "column-store variable-size leaf pages" : 0, "fixed-record size" : 0, "maximum internal page key size" : 368, "maximum internal page size" : 4096, "maximum leaf page key size" : 2867, "maximum leaf page size" : 32768, "maximum leaf page value size" : 67108864, "maximum tree depth" : 3, "number of key/value pairs" : 0, "overflow pages" : 0, "pages rewritten by compaction" : 0, "row-store empty values" : 0, "row-store internal pages" : 0, "row-store leaf pages" : 0 }, "cache" : { "bytes currently in the cache" : 19401033, "bytes dirty in the cache cumulative" : 28415209, "bytes read into cache" : 0, "bytes written from cache" : 31893695, "checkpoint blocked page eviction" : 0, "data source pages selected for eviction unable to be evicted" : 0, "eviction walk passes of a file" : 0, "eviction walk target pages histogram - 0-9" : 0, "eviction walk target pages histogram - 10-31" : 0, "eviction walk target pages histogram - 128 and higher" : 0, "eviction walk target pages histogram - 32-63" : 0, "eviction walk target pages histogram - 64-128" : 0, "eviction walks abandoned" : 0, "eviction walks gave up because they restarted their walk twice" : 0, "eviction walks gave up because they saw too many pages and found no candidates" : 0, "eviction walks gave up because they saw too many pages and found too few candidates" : 0, "eviction walks reached end of tree" : 0, "eviction walks started from root of tree" : 0, "eviction walks started from saved location in tree" : 0, "hazard pointer blocked page eviction" : 0, "in-memory page passed criteria to be split" : 4, "in-memory page splits" : 2, "internal pages evicted" : 0, "internal pages split during eviction" : 0, "leaf pages split during eviction" : 2, "modified pages evicted" : 2, "overflow pages read into cache" : 0, "page split during eviction deepened the tree" : 0, "page written requiring cache overflow records" : 0, "pages read into cache" : 0, "pages read into cache after truncate" : 1, "pages read into cache after truncate in prepare state" : 0, "pages read into cache requiring cache overflow entries" : 0, "pages requested from the cache" : 76607, "pages seen by eviction walk" : 0, "pages written from cache" : 552, "pages written requiring in-memory restoration" : 0, "tracked dirty bytes in the cache" : 0, "unmodified pages evicted" : 0 }, "cache_walk" : { "Average difference between current eviction generation when the page was last considered" : 0, "Average on-disk page image size seen" : 0, "Average time in cache for pages that have been visited by the eviction server" : 0, "Average time in cache for pages that have not been visited by the eviction server" : 0, "Clean pages currently in cache" : 0, "Current eviction generation" : 0, "Dirty pages currently in cache" : 0, "Entries in the root page" : 0, "Internal pages currently in cache" : 0, "Leaf pages currently in cache" : 0, "Maximum difference between current eviction generation when the page was last considered" : 0, "Maximum page size seen" : 0, "Minimum on-disk page image size seen" : 0, "Number of pages never visited by eviction server" : 0, "On-disk page image sizes smaller than a single allocation unit" : 0, "Pages created in memory and never written" : 0, "Pages currently queued for eviction" : 0, "Pages that could not be queued for eviction" : 0, "Refs skipped during cache traversal" : 0, "Size of the root page" : 0, "Total number of pages currently in cache" : 0 }, "compression" : { "compressed page maximum internal page size prior to compression" : 4096, "compressed page maximum leaf page size prior to compression " : 111392, "compressed pages read" : 0, "compressed pages written" : 548, "page written failed to compress" : 0, "page written was too small to compress" : 4 }, "cursor" : { "bulk loaded cursor insert calls" : 0, "cache cursors reuse count" : 74, "close calls that result in cache" : 0, "create calls" : 4, "insert calls" : 50718, "insert key and value bytes" : 24500283, "modify" : 0, "modify key and value bytes affected" : 0, "modify value bytes modified" : 0, "next calls" : 101747, "open cursor count" : 0, "operation restarted" : 0, "prev calls" : 1, "remove calls" : 0, "remove key bytes removed" : 0, "reserve calls" : 0, "reset calls" : 51000, "search calls" : 25359, "search near calls" : 25485, "truncate calls" : 0, "update calls" : 0, "update key and value bytes" : 0, "update value size change" : 0 }, "reconciliation" : { "dictionary matches" : 0, "fast-path pages deleted" : 0, "internal page key bytes discarded using suffix compression" : 1077, "internal page multi-block writes" : 1, "internal-page overflow keys" : 0, "leaf page key bytes discarded using prefix compression" : 0, "leaf page multi-block writes" : 12, "leaf-page overflow keys" : 0, "maximum blocks required for a page" : 1, "overflow values written" : 0, "page checksum matches" : 0, "page reconciliation calls" : 80, "page reconciliation calls for eviction" : 2, "pages deleted" : 0 }, "session" : { "object compaction" : 0 }, "transaction" : { "update conflicts" : 0 } }, "nindexes" : 4, "indexBuilds" : [ ], "totalIndexSize" : 704512, "totalSize" : 10375168, "indexSizes" : { "_id_" : 241664, "cuisine_1" : 147456, "borough_1_cuisine_1" : 159744, "borough_1_address.zipcode_1" : 155648 }, "scaleFactor" : 1, "ok" : 1 }
統計に対してスケーリング パラメーターが指定されていないため、すべてのサイズ値は bytes にあります。
単位を指定する統計検索
次の操作では、1024 の scale を指定することでデータの単位を bytes から kilobytes に変更します。
db.restaurants.stats( { scale : 1024 } )
この操作では以下が返されます。
{ "ns" : "test.restaurants", "size" : 7380, "count" : 25359, "avgObjSize" : 298, "storageSize" : 9444, "freeStorageSize" : 6504, "capped" : false, "wiredTiger" : { ... }, "nindexes" : 4, "indexBuilds" : [ ], "totalIndexSize" : 688, "totalSize" : 10132, "indexSizes" : { "_id_" : 236, "cuisine_1" : 144, "borough_1_cuisine_1" : 156, "borough_1_address.zipcode_1" : 152 }, "scaleFactor" : 1024, "ok" : 1 }
インデックスの詳細を含む統計検索
次の操作では、コレクション内の各インデックスに関連する情報を含む indexDetails ドキュメントが作成されます。
db.restaurants.stats( { indexDetails : true } )
この操作では以下が返されます。
{ "ns" : "test.restaurants", "size" : 7557340, "count" : 25359, "avgObjSize" : 298, "storageSize" : 9670656, "freeStorageSize" : 6660096, "capped" : false, "wiredTiger" : { ... }, "nindexes" : 4, "indexDetails" : { "_id_" : { "metadata" : { "formatVersion" : 8 }, "creationString" : "access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=8),assert=(commit_timestamp=none,durable_timestamp=none,read_timestamp=none),block_allocation=best,block_compressor=,cache_resident=false,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=false,extractor=,format=btree,huffman_key=,huffman_value=,ignore_in_memory_cache_size=false,immutable=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,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=true),lsm=(auto_throttle=true,bloom=true,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=false,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_custom=(prefix=,start_generation=0,suffix=),merge_max=15,merge_min=0),memory_page_image_max=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=90,type=file,value_format=u", "type" : "file", "uri" : "statistics:table:index-44-6797839137631957852", "LSM" : { ... }, "block-manager" : { ... }, "btree" : { ... }, "cache" : { ... }, "cache_walk" : { ... }, "compression" : { ... }, "cursor" : { ... }, "reconciliation" : { ... }, "session" : { ... }, "transaction" : { ... } }, "cuisine_1" : { ... }, "borough_1_cuisine_1" : { ... }, "borough_1_address.zipcode_1" : { ... }, "indexBuilds" : [ ], "totalIndexSize" : 704512, "totalSize" : 10375168, "indexSizes" : { "_id_" : 241664, "cuisine_1" : 147456, "borough_1_cuisine_1" : 159744, "borough_1_address.zipcode_1" : 155648 }, "scaleFactor" : 1, "ok" : 1 }
インデックス詳細のフィルタリングによる統計検索
indexDetails フィールドのインデックスをフィルタリングするには、indexDetailsKey オプションを使用してインデックス キーを指定するか、indexDetailsName を使用してインデックス名を指定します。コレクションのインデックス キーと名前を検索するには、db.collection.getIndexes() を使用します。
次のインデックスが与えられている場合。
{ "ns" : "test.restaurants", "v" : 1, "key" : { "borough" : 1, "cuisine" : 1 }, "name" : "borough_1_cuisine_1" }
次の操作は、indexDetails ドキュメントを indexDetailsKey ドキュメントで定義された単一のインデックスでフィルタリングします。
db.restaurants.stats( { 'indexDetails' : true, 'indexDetailsKey' : { 'borough' : 1, 'cuisine' : 1 } } )
次の操作は、indexDetails ドキュメントを indexDetailsName ドキュメントで定義された単一のインデックスでフィルタリングします。
db.restaurants.stats( { 'indexDetails' : true, 'indexDetailsName' : 'borough_1_cuisine_1' } )
どちらの操作も同じ出力を返します。
{ "ns" : "test.restaurants", "size" : 7557340, "count" : 25359, "avgObjSize" : 298, "storageSize" : 9670656, "freeStorageSize" : 6660096, "capped" : false, "wiredTiger" : { ... }, "nindexes" : 4, "indexDetails" : { "borough_1_cuisine_1" : { "metadata" : { "formatVersion" : 8 }, "LSM" : { ... }, "block-manager" : { ... }, "btree" : { ... }, "cache" : { ... }, "cache_walk" : { ... }, "compression" : { ... }, "cursor" : { ... }, "reconciliation" : { ... }, "session" : { ... }, "transaction" : { ... } } }, "indexBuilds" : [ ], "totalIndexSize" : 704512, "totalSize" : 10375168, "indexSizes" : { "_id_" : 241664, "cuisine_1" : 147456, "borough_1_cuisine_1" : 159744, "borough_1_address.zipcode_1" : 155648 }, "scaleFactor" : 1, "ok" : 1 }
出力の説明については、出力の詳細を参照してください。