Terminate Running Operations终止正在运行的操作
On this page本页内容
Overview概述
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部署中操作的行为。
Available Procedures可用程序
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会在下一个中断点中断操作。
Terminate a Query终止查询
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)
Terminate a Command终止命令
Consider a potentially long running operation using 考虑一个可能长期运行的操作,使用distinct
to return each distinct collection
field that has a city
key:distinct
返回每个具有城市键的剔除重复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()
identifies the target operation by operation ID.db.killOp()
方法在下一个中断点中断正在运行的操作。db.killOp()
通过操作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()
终止客户端启动的操作,而不终止内部数据库操作。
Sharded Cluster分片集群
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
参数。