On this page本页内容
MongoDB provides two facilitates to terminate running operations: MongoDB提供了两种终止运行操作的方法:maxTimeMS()
and db.killOp()
. maxTimeMS()
和db.killOp()
。Use these operations as needed to control the behavior of operations in a MongoDB deployment.根据需要使用这些操作来控制MongoDB部署中的操作行为。
maxTimeMS
The maxTimeMS()
method sets a time limit for an operation. maxTimeMS()
方法设置操作的时间限制。When the operation reaches the specified time limit, MongoDB interrupts the operation at the next interrupt point.当操作达到指定的时间限制时,MongoDB在下一个中断点中断操作。
From 在mongosh
, use the following method to set a time limit of 30 milliseconds for this query:mongosh
中,使用以下方法为此查询设置30毫秒的时间限制:
db.location.find( { "town": { "$regex": "(Pine Lumber)", "$options": 'i' } } ).maxTimeMS(30)
Consider a potentially long running operation using 考虑一个可能长时间运行的操作,使用distinct
to return each distinct collection
field that has a city
key:distinct
返回具有city
键的每个不同collection
字段:
db.runCommand( { distinct: "collection", key: "city" } )
You can add the 您可以将maxTimeMS
field to the command document to set a time limit of 45 milliseconds for the operation:maxTimeMS
字段添加到命令文档中,为操作设置45毫秒的时间限制:
db.runCommand( { distinct: "collection", key: "city", maxTimeMS: 45 } )
Operations that reach 达到maxTimeMS
will return a MaxTimeMSExpired
error.maxTimeMS
的操作将返回MaxTimeMSExpired错误。
killOp
The db.killOp()
method interrupts a running operation at the next interrupt point. db.killOp()
方法在下一个中断点中断正在运行的操作。db.killOp()
identifies the target operation by operation ID.通过操作ID标识目标操作。
db.killOp(<opId>)
Terminate running operations with extreme caution. 非常小心地终止运行操作。Only use 仅使用db.killOp()
to terminate operations initiated by clients and do not terminate internal database operations.db.killOp()
终止客户端启动的操作,而不终止内部数据库操作。
Starting in MongoDB 4.0, the 从MongoDB 4.0开始,killOp
command can be run on a mongos
and can kill queries (i.e. read operations)
that span shards in a cluster. killOp
命令可以在mongos
上运行,并可以终止跨集群中分片的查询(即读取操作)。The 当要终止的操作是写操作时,来自killOp
command from the mongos
does not propagate to the shards when the operation to be killed is a write operation.mongos
的killOp
命令不会传播到分片。
For more information on killing operations on a sharded cluster, see:有关在分片群集上执行终止操作的详细信息,请参阅:
For information on how to list sharding operations that are active on a 有关如何列出mongos
, see the localOps
parameter in $currentOp
.mongos
上活动的分片操作的信息,请参阅$currentOp
中的localOps
参数。