Definition定义
cursor.noCursorTimeout()-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。Instructs the server to avoid closing a cursor automatically after a period of inactivity.指示服务器避免在一段时间不活动后自动关闭游标。ThenoCursorTimeout()method has the following prototype form:noCursorTimeout()方法具有以下原型形式:db.collection.find(<query>).noCursorTimeout()
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Important
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Behavior行为
Session Idle Timeout Overrides noCursorTimeout会话空闲超时覆盖noCursorTimeout
noCursorTimeoutMongoDB drivers and MongoDB驱动程序和mongosh associate all operations with a server session, with the exception of unacknowledged write operations. mongosh将所有操作与服务器会话相关联,但未确认的写入操作除外。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.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. 如果会话空闲时间超过30分钟,MongoDB服务器会将该会话标记为已过期,并可能随时将其关闭。当MongoDB服务器关闭会话时,它还会终止任何正在进行的操作并打开与会话关联的游标。This includes cursors configured with 这包括配置了noCursorTimeout() or a maxTimeMS() greater than 30 minutes.noCursorTimeout()或大于30分钟的maxTimeMS()的游标。
Consider an application that issues a 考虑一个应用程序,它发出带有db.collection.find() with cursor.noCursorTimeout(). cursor.noCursorTimeout()的db.collection.find()。The server returns a cursor along with a batch of documents defined by the 服务器返回一个游标以及由cursor.batchSize() of the find(). find()的cursor.batchSize()定义的一批文档。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. 每次应用程序向服务器请求一批新文档时,会话都会刷新。但是,如果应用程序处理当前一批文档的时间超过30分钟,则会话将标记为已过期并关闭。When the server closes the session, it also kills the cursor despite the cursor being configured with 当服务器关闭会话时,它也会杀死游标,尽管游标配置了noCursorTimeout(). noCursorTimeout()。When the application requests the next batch of documents, the server returns an error.当应用程序请求下一批文档时,服务器返回错误。
Refresh a Cursor with refreshSessions使用refreshSessions刷新游标
refreshSessionsFor operations that return a cursor, if the cursor may be idle for longer than 30 minutes, issue the operation within an explicit session using 对于返回游标的操作,如果游标可能空闲超过30分钟,请使用Mongo.startSession() and periodically refresh the session using the refreshSessions command. For example: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. db.collection.find()方法与显式会话相关联。The cursor is configured with 游标配置了cursor.noCursorTimeout() to prevent the server from closing the cursor if idle. cursor.noCursorTimeout(),以防止服务器在空闲时关闭游标。The while loop includes a block that uses refreshSessions to refresh the session every 5 minutes. while循环包含一个块,该块使用refreshSessions每5分钟刷新一次会话。Since the session will never exceed the 30 minute idle timeout, the cursor can remain open indefinitely.由于会话永远不会超过30分钟的空闲超时,因此游标可以无限期保持打开状态。
For MongoDB drivers, defer to the driver documentation for instructions and syntax for creating sessions.对于MongoDB驱动程序,请参阅驱动程序文档以获取创建会话的说明和语法。