定義
cursor.maxTimeMS(<time limit>)重要
mongosh メソッド
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.MongoDB API ドライバーについては、各言語の「MongoDB ドライバーのドキュメント」を参照してください。
カーソルに対する情報処理操作の累積時間制限をミリ秒単位で指定します。
The
maxTimeMS()method has the following prototype form:db.collection.find( { <query> }, { <projection> } ).maxTimeMS( <milliseconds> ) The
maxTimeMS()method has the following parameter:Parameterタイプ説明millisecondsinteger
カーソルに対する処理操作の累積時間制限をミリ秒単位で指定します。
重要
maxTimeMS() is not related to the NoCursorTimeout query flag. maxTimeMS() relates to processing time, while NoCursorTimeout relates to idle time. A cursor's idle time does not contribute towards its processing time.
maxAwaitTimeMS() メソッドは追尾可能 (tailable) カーソルが次の応答を待機する時間の制限を設定します。合計処理時間に制限はありません。
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
- 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 のバージョン
動作
MongoDB は、関連付けられたカーソルが割り当てられた時間制限を超えた場合に、操作を終了の対象とします。MongoDB は、db.killOp() と同じメカニズムを使用して、割り当てられた時間制限を超えた操作を終了します。MongoDB は、指定された割り込みポイントのいずれかでのみ操作を終了します。
MongoDB は、クライアントとサーバー間のレイテンシをカーソルの時間制限にカウントしません。ただし、シャーディングされたクラスターの場合、MongoDB は mongos インスタンスと mongod インスタンス間のレイテンシをこの時間制限に含めます。
複数の結果のバッチを生成するクエリは、カーソルが割り当てられた制限時間を超えるまでバッチを返し続けます。
セッション アイドル タイムアウトの上書き maxTimeMS
MongoDB drivers and mongosh associate all operations with a server session, with the exception of unacknowledged write operations. For operations not explicitly associated with a session (i.e. using Mongo.startSession()), MongoDB drivers and mongosh create an implicit session and associate it with the operation.
If a session is idle for longer than 30 minutes, the MongoDB server marks that session as expired and may close it at any time. When the MongoDB server closes the session, it also kills any in-progress operations and open cursors associated with the session. This includes cursors configured with noCursorTimeout() or a maxTimeMS() greater than 30 minutes.
For example, consider a find() operation with the maxTimeMS() configured for a timeout of 31 minutes. The server returns a cursor along with a batch of documents defined by the cursor.batchSize() of the find(). The session refreshes each time the application requests a new batch of documents from the server. However, if the application takes longer than 30 minutes to process the current batch of documents, the session is marked as expired and closed. When the server closes the session, it also kills the cursor despite the cursor being configured with maxTimeMS() greater than 30 minutes. When the application requests the next batch of documents, the server returns an error.
カーソルを返す操作の場合、カーソルが 30 分以上アイドル状態になる可能性がある場合は、 Mongo.startSession()を使用して明示的なセッション内で操作を発行し、 refreshSessionsコマンドを使用して定期的にセッションを更新します。以下がその例です。
var session = db.getMongo().startSession() var sessionId = session sessionId // show the sessionId var cursor = session.getDatabase("examples").getCollection("data").find().noCursorTimeout() var refreshTimestamp = new Date() // take note of time at operation start while (cursor.hasNext()) { // Check if more than 5 minutes have passed since the last refresh if ( (new Date()-refreshTimestamp)/1000 > 300 ) { print("refreshing session") db.adminCommand({"refreshSessions" : [sessionId]}) refreshTimestamp = new Date() } // process cursor normally }
In the example operation, the db.collection.find() method is associated with an explicit session. The cursor is configured with cursor.maxTimeMS() to keep the cursor open for at least 31 minutes. The while loop includes a block that uses refreshSessions to refresh the session every 5 minutes. Since the session will never exceed the 30 minute idle timeout, the cursor can remain open up to the configured maxTimeMS().
For MongoDB drivers, defer to the driver documentation for instructions and syntax for creating sessions.
すべての操作のデフォルトタイムアウト
Starting in MongoDB 8.0, you can use the defaultMaxTimeMS cluster parameter to specify a default time limit for individual read operations to complete. If a query specifies a maxTimeMS() option, that value overrides the defaultMaxTimeMS value.
例
例
次のクエリでは、50 ミリ秒の時間制限を指定しています。
db.collection.find({description: /August [0-9]+, 1969/}).maxTimeMS(50)