MongoDB とドライバー
定義
db.collection.estimatedDocumentCount(options)コレクションまたはビュー内の全ドキュメントの数を返します。
db.collection.estimatedDocumentCount( <options> )
パラメーター
Parameter | タイプ | 説明 |
|---|---|---|
options | ドキュメント | 任意。カウント動作に影響を与える追加のオプション。 |
フィールド
optionsドキュメントには以下の内容を含めることができます。
フィールド | タイプ | 説明 |
|---|---|---|
| integer | 任意。カウントの実行を許可する最大時間。 |
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
動作
Mechanics
db.collection.estimatedDocumentCount() は、クエリフィルターは使用せず、代わりにメタデータを使用してコレクションの数を返します。
ビューの場合、以下のようになります。
メタデータはありません。
ドキュメント数は、ビュー定義で集計パイプラインを実行することによって計算されます。
ドキュメント数を迅速に見積もることはできません。
シャーディングされたクラスター
シャーディングされたクラスターでは、結果のカウントで孤立したドキュメントが正しく除外されません。
不正なシャットダウン
このセクションは、コレクションにのみ適用されます。
不正なシャットダウンの後は、カウントが不正確になる可能性があります。
After an unclean shutdown of a mongod using the Wired Tiger storage engine, count statistics reported by db.collection.estimatedDocumentCount() may be inaccurate.
ドリフトの量は、チェックポイントからクリーン シャットダウンまでの間に実行された挿入、アップデート、または削除操作の数によって異なります。チェックポイントは通常、60 秒ごとに発現します。ただし、デフォルト以外の --syncdelay 設定で実行されている mongod インスタンスでは、チェックポイントの頻度が増減する可能性があります。
不正なシャットダウン後に統計を復元するには、mongod の各コレクションに対して validate を実行します。
不正なシャットダウン後:
クライアントの切断
If the client that issued db.collection.estimatedDocumentCount() disconnects before the operation completes, MongoDB marks db.collection.estimatedDocumentCount() for termination using killOp.
カウントとトランザクション
When you use db.collection.estimatedDocumentCount() in a transaction, the resulting count will not filter out any uncommitted multi-document transactions.
例
このページの例では、sample_mflix データセット のデータを使用しています。自己管理型 MongoDB 配置にこのデータセットをロードする方法の詳細については、「サンプル データセットのロード」を参照してください。サンプル データベースに何らかの変更を加えた場合は、今ページの例を実行するには、データベースを削除して再作成する必要がある可能性があります。
The following example uses db.collection.estimatedDocumentCount() to retrieve the count of all documents in the movies collection:
db.movies.estimatedDocumentCount({})
21349