Docs Menu

cursor.noCursorTimeout()

cursor.noCursorTimeout()

重要

mongosh メソッド

このページでは、mongosh メソッドについて記載しています。これは Node.js などの言語固有のドライバーのドキュメントではありません

MongoDB API ドライバーについては、各言語の MongoDB ドライバー ドキュメントを参照してください。

Instructs the server to avoid closing a cursor automatically after a period of inactivity.

noCursorTimeout() メソッドのプロトタイプ形式は次のとおりです。

db.collection.find(<query>).noCursorTimeout()

このメソッドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

重要

このコマンドは、M0、M2、M5、および Flex クラスターではサポートされていません。詳細については、「 サポートされていないコマンド 」を参照してください。

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

MongoDB ドライバーとmongosh では、確認されていない書込み操作を除くすべての操作がサーバー セッションに関連付けられます。セッションに明示的に関連付けられていない操作(つまり Mongo.startSession()を使用するもの)の場合、MongoDB ドライバーとmongoshによって暗黙的なセッションが作成され、それが操作に関連付けられます。

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.

Consider an application that issues a db.collection.find() with cursor.noCursorTimeout(). 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 noCursorTimeout(). 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.noCursorTimeout() to prevent the server from closing the cursor if idle. 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 indefinitely.

MongoDB ドライバーの場合、セッションを作成するための手順と構文については、ドライバーのドキュメントを参照してください。