MongoDB 4.4 db.currentOp() 和 db.killOp() 方法说明


本文链接:https://www.cndba.cn/dave/article/107987

https://www.mongodb.com/docs/manual/reference/method/db.currentOp/

db.currentOp()方法会返回数据库实例当前操作的信息,是数据库命令currentOp的封装。

语法:

db.currentOp( )

operations 是可选项。 可以是boolean 或者 document 类型。 当指定true时,会包含空闲连接和系统操作。当指定查询条件的filter文档时,则只返回匹配条件的操作。

db.currentOp()支持的filter文档有如下3种类型:

  1. “$ownOps”: 布尔型,当设置为true时,只返回当前用户的操作信息。
  2. “$all”:布尔型,当设置为true时,返回所有操作的信息,包括空闲连接和系统操作。
  3. :根据output fields指定过滤条件。 当”$all”: true 和 output fields 同时存在时,只有”$all”: true生效。

完整的output fields 参考官网链接:

https://www.mongodb.com/docs/manual/reference/command/currentOp/#std-label-currentOp-output-fields

db.currentOp(true) 和 db.currentOp( { “$all”: true } ) 是等效的,查询结果相同。

Db.currentOp() 和 database profiler 一样对所有的CRUD操作输出相同的基本诊断信息,具体如下:

  1. aggregate
  2. count
  3. delete
  4. distinct
  5. find (OP_QUERY and command)
  6. findAndModify
  7. getMore (OP_GET_MORE and command)
  8. insert
  9. mapReduce
  10. update

这些操作也同样会记录在慢查询日志中(slowOpThresholdMs )。

MongoDB 4.4 数据库分析器(Database Profiler)
https://www.cndba.cn/dave/article/107982

https://www.mongodb.com/docs/manual/reference/command/currentOp/#std-label-currentOp-output-fields

https://www.mongodb.com/docs/manual/reference/method/db.killOp/

db.killOp(opid) 方法根据指定的操作ID 来中断操作。 可以使用db.currentOp()来查看opid。

中断正在运行的操作是一项非常危险的操作,只能用db.killOp() 方法来中断客户端的操作,不能用来中断数据库内部的操作。

https://www.mongodb.com/docs/manual/reference/server-sessions/

如果写操作已经关联到了server session,那么可以在mongos 节点执行killSessions 命令来kill 跨shard的写操作。

MongoDB Shell (mongosh) 工具说明
  • 下一篇:Mongo Shell 的语句块操作