정의
cursor.noCursorTimeout()중요
Mongo쉬 방법
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.MongoDB API 드라이버의 경우 언어별 MongoDB 드라이버 설명서를 참조하세요.
일정 시간 동안 활동이 없어도 커서가 자동으로 닫히지 않도록 서버에 지시합니다.
The
noCursorTimeout()method has the following prototype form:db.collection.find(<query>).noCursorTimeout()
호환성
이 메서드는 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
중요
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.
MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전
MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전
행동
세션 유휴 시간 제한 재정의 noCursorTimeout
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.
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.
커서 새로 고침 refreshSessions
커서를 반환하는 작업의 경우 커서가 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.
For MongoDB drivers, defer to the driver documentation for instructions and syntax for creating sessions.