cursor.maxTimeMS()

On this page本页内容

Definition定义

cursor.maxTimeMS(<time limit>)

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

Specifies a cumulative time limit in milliseconds for processing operations on a cursor.指定处理游标上的操作的累积时间限制(以毫秒为单位)。

The maxTimeMS() method has the following parameter:maxTimeMS()方法具有以下参数:

Parameter参数Type类型Description描述
milliseconds integer Specifies a cumulative time limit in milliseconds for processing operations on the cursor.指定在游标上处理操作的累积时间限制(以毫秒为单位)。

Important

maxTimeMS() is not related to the NoCursorTimeout query flag.maxTimeMS()NoCursorTimeout查询标志无关。maxTimeMS() relates to processing time, while NoCursorTimeout relates to idle time. maxTimeMS()与处理时间有关,而NoCursorTimeout与空闲时间有关。A cursor’s idle time does not contribute towards its processing time.游标的空闲时间不会影响其处理时间。

Behaviors行为

MongoDB targets operations for termination if the associated cursor exceeds its allotted time limit. 如果相关游标超过其分配的时间限制,MongoDB将终止操作。MongoDB terminates operations that exceed their allotted time limit using the same mechanism as db.killOp(). MongoDB使用与db.killOp()相同的机制终止超过其分配时间限制的操作。MongoDB only terminates an operation at one of its designated interrupt points.MongoDB只在其指定的中断点之一终止操作。

MongoDB does not count network latency between the client and the server towards a cursor’s time limit. MongoDB不计算客户端和服务器之间的网络延迟,以达到游标的时间限制。For a sharded cluster, however, MongoDB does include the latency between the mongos and mongod instances towards this time limit.然而,对于分片集群,MongoDB确实包括mongosmongod实例之间在这个时间限制内的延迟。

Queries that generate multiple batches of results continue to return batches until the cursor exceeds its allotted time limit.生成多批结果的查询将继续返回批,直到游标超过其分配的时间限制。

Session Idle Timeout Overrides 会话空闲超时覆盖maxTimeMS

Starting in MongoDB 3.6, MongoDB drivers and the mongo shell associate all operations with a server session, with the exception of unacknowledged write operations. 从MongoDB 3.6开始,MongoDB驱动程序和mongo shell将所有操作与服务器会话相关联,但未确认的写入操作除外。For operations not explicitly associated with a session (i.e. using Mongo.startSession()), MongoDB drivers and the mongo shell creates an implicit session and associates it with the operation.对于未显式与会话关联的操作(即使用Mongo.startSession()),MongoDB驱动程序和mongo shell会创建一个隐式会话,并将其与操作关联。

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. 如果会话空闲时间超过30分钟,MongoDB服务器会将该会话标记为已过期,并可随时将其关闭。When the MongoDB server closes the session, it also kills any in-progress operations and open cursors associated with the session. 当MongoDB服务器关闭会话时,它还会终止任何正在进行的操作以及与会话相关的打开游标。This includes cursors configured with noCursorTimeout or a maxTimeMS greater than 30 minutes.这包括配置了noCursorTimeoutmaxTimeMS(大于30分钟)的游标。

For example, consider a find() operation with the maxTimeMS configured for a timeout of 31 minutes. 例如,考虑使用maxTimeMS配置的find()操作,用于超时31分钟。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 maxTimeMS greater than 30 minutes. 当服务器关闭会话时,它也会杀死游标,尽管游标配置的maxTimeMS大于30分钟。When the application requests the next batch of documents, the server returns an error.当应用程序请求下一批文档时,服务器返回一个错误。

For operations that return a cursor, if the cursor may be idle for longer than 30 minutes, issue the operation within an explicit session using Session.startSession() and periodically refresh the session using the refreshSessions command. 对于返回游标的操作,如果游标空闲时间可能超过30分钟,请使用Session.startSession()在显式会话中发出该操作,并使用refreshSessions命令定期刷新会话。For example:例如:

var session = db.getMongo().startSession()
var sessionId = session.getSessionId().id

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.maxTimeMS() to keep the cursor open for at least 31 minutes. 游标配置为cursor.maxTimeMS(),以使游标保持打开状态至少31分钟。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 up to the configured maxTimeMS().由于会话永远不会超过30分钟空闲超时,因此游标可以保持打开状态,直到配置的maxTimeMS()

For MongoDB drivers, defer to the driver documentation for instructions and syntax for creating sessions.对于MongoDB驱动程序,请按照驱动程序文档获取创建会话的说明和语法。

See also参阅

Session Idle Timeout

Examples示例

Example

The following query specifies a time limit of 50 milliseconds:以下查询指定了50毫秒的时间限制:

db.collection.find({description: /August [0-9]+, 1969/}).maxTimeMS(50)