定義
cursor.allowDiskUse()重要
mongosh メソッド
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。
Use
allowDiskUse()to either allow or prohibit writing temporary files on disk when a pipeline stage exceeds the 100 megabyte limit. Starting in MongoDB 6.0, operations that require greater than 100 megabytes of memory automatically write data to temporary files by default.allowDiskUse()の形式は次のとおりです。db.collection.find(<match>).sort(<sort>).allowDiskUse()
メモリ内ソート操作の詳細については、「ソートとインデックスの使用」を参照してください。
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- 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 のバージョン
動作
相互作用: allowDiskUseByDefault
MongoDB 6.0 以降、実行に必要とされるメモリが 100 MB を超えるパイプライン ステージでは、デフォルトで一時ファイルをディスクに書き込みます。
注意
MongoDB 6.0 より前のバージョンでは、.allowDiskUse(false) と .allowDiskUse(true) は同じ効果です。MongoDB 6.0 では、mongosh とレガシー mongo shell はどちらも次のように動作します。
allowDiskUseByDefault が true の場合(これがデフォルト):
.allowDiskUse()に追加効果はない.allowDiskUse(true)に追加効果はない.allowDiskUse(false)はクエリが一時ファイルをディスクに書き込むことを禁止する
allowDiskUseByDefault が false の場合:
.allowDiskUse()ディスクへの一時ファイルの書き込みが可能.allowDiskUse(true)ディスクへの一時ファイルの書き込みが可能.allowDiskUse(false)に追加効果はない
インデックスなしの大きなソートのみをサポート
cursor.allowDiskUse() has no effect on sort operations answered using an index or non-indexed ("in-memory") sort operations which require less than 100 megabytes of memory. For more complete documentation on in-memory sorts and sort index use, see Sort and Index Use.
MongoDB がインメモリソートを実行する必要があるかを確認するには、クエリに cursor.explain() を追加し、explain の結果 を確認します。クエリプランに SORT ステージが含まれている場合、MongoDB は 100 メガバイトのメモリ制限の下でインメモリソートを実行する必要があります。
例
_id のデフォルト インデックスのみを持つコレクション sensors を考えてみましょう。コレクションには、以下と同様のドキュメントが含まれています。
{ "sensor-name" : "TEMP-21425", "sensor-location" : "Unit 12", "reading" : { "timestamp" : Timestamp(1580247215, 1), "value" : 212, "unit" : "Fahrenheit" } }
The following operation includes a cursor.sort() on the field reading.timestamp. The operation also passes false to cursor.allowDiskUse() to prohibit the query from writing temporary files to disk.
db.sensors.find({"sensor-location" : "Unit 12"}). sort({"reading.timestamp" : 1}). allowDiskUse(false)
reading.timestampがインデックスに含まれていないため、MongoDBは要求された並べ替え順序で結果を返すためにメモリ内でソート操作を実行する必要があります。cursor.allowDiskUse(false) を指定すると、MongoDB はソート操作に 100 メガバイトを超えるシステムメモリが必要な場合、そのソート処理を実行できなくなります。操作に 100 メガバイトを超えるシステムメモリが必要な場合、MongoDB はエラーを返します。