currentOp

在本页面

Definition

currentOp的格式如下:

{ currentOp: 1 }

mongo shell 为currentOp命令提供db.currentOp()包装器。

Behavior

currentOp必须针对admin数据库运行,并且它可以接受几个可选字段。

Field Description
"$ownOps" 布尔值。如果设置为true,则仅返回有关当前用户操作的信息。


mongod个实例上,用户始终被授权以"$ownOps": true的身份运行currentOp来查看自己的操作。参见access control
3.2.9 版中的新功能。
| "$all" |布尔值。如果设置为true,则返回有关所有操作的信息,包括有关空闲连接的操作和系统操作。
"$all": true会覆盖所有输出字段过滤器。
|<filter> |在Output Fields上指定过滤条件。参见Examples

currentOpdatabase profiler报告所有 CRUD 操作的相同基本诊断信息,包括以下内容:

这些操作也包含在慢查询的日志 Logging(有关慢查询的日志的更多信息,请参见slowOpThresholdMs)。

Access Control

在以authorization运行的系统上,用户必须具有包括inprog特权操作的访问权限。

从 3.2.9 开始,用户可以使用mongod实例上的$ownOps来查看自己的操作,而无需inprog特权操作。

db.adminCommand( { currentOp: 1, "$ownOps": 1 } )

Examples

以下示例将currentOp命令与各种查询文档一起使用以过滤输出。

显示所有当前操作

db.adminCommand(
   {
     currentOp: true,
     "$all": true
   }
)

await 锁定的写操作

下面的示例返回有关所有 await 锁定的写操作的信息:

在版本 3.6 中更改。

db.adminCommand(
   {
     currentOp: true,
     "waitingForLock" : true,
     $or: [
        { "op" : { "$in" : [ "insert", "update", "remove" ] } },
        { "command.findandmodify": { $exists: true } }
    ]
   }
)

没有收益的有效操作

以下示例返回从未产生的所有活动运行操作的信息:

db.adminCommand(
   {
     currentOp: true,
     "active" : true,
     "numYields" : 0,
     "waitingForLock" : false
   }
)

特定数据库上的活动操作

以下示例返回有关运行时间超过 3 秒的数据库db1的所有活动操作的信息:

db.adminCommand(
   {
     currentOp: true,
     "active" : true,
     "secs_running" : { "$gt" : 3 },
     "ns" : /^db1\./
   }
)

有效的索引编制操作

在版本 3.6 中更改。

以下示例返回有关索引创建操作的信息:

db.adminCommand(
    {
      currentOp: true,
      $or: [
        { op: "command", "command.createIndexes": { $exists: true }  },
        { op: "none", "msg" : /^Index Build/ }
      ]
    }
)

Output Example

Standalone
Replica Set (Primary)
Sharded Cluster (mongos)

The following is a prototype of the currentOp output when run on a standalone:

{
  "inprog": [
       {
         "host" : <string>,
         "desc" : <string>,
         "connectionId" : <number>,
         "client" : <string>,
         "appName" : <string>,
         "clientMetadata" : <document>,
         "active" : <boolean>,
         "currentOpTime" : <string>,
         "opid" : <number>,
         "secs_running" : <NumberLong()>,
         "microsecs_running" : <number>,
         "op" : <string>,
         "ns" : <string>,
         "command" : <document>,
         "originatingCommand" : <document>,
         "planSummary": <string>,
         "msg": <string>,
         "progress" : {
             "done" : <number>,
             "total" : <number>
         },
         "killPending" : <boolean>,
         "numYields" : <number>,
         "locks" : {
             "Global" : <string>,
             "MMAPV1Journal" : <string>,
             "Database" : <string>,
             "Collection" : <string>,
             "Metadata" : <string>,
             "oplog" : <string>
         },
         "waitingForLock" : <boolean>,
         "lockStats" : {
             "Global": {
                "acquireCount": {
                   "r": <NumberLong>,
                   "w": <NumberLong>,
                   "R": <NumberLong>,
                   "W": <NumberLong>
                },
                "acquireWaitCount": {
                   "r": <NumberLong>,
                   "w": <NumberLong>,
                   "R": <NumberLong>,
                   "W": <NumberLong>
                },
                "timeAcquiringMicros" : {
                   "r" : NumberLong(0),
                   "w" : NumberLong(0),
                   "R" : NumberLong(0),
                   "W" : NumberLong(0)
                },
                "deadlockCount" : {
                   "r" : NumberLong(0),
                   "w" : NumberLong(0),
                   "R" : NumberLong(0),
                   "W" : NumberLong(0)
                }
             },
             "MMAPV1Journal": {
                ...
             },
             "Database" : {
                ...
             },
             ...
         }
       },
       ...
   ],
   "fsyncLock": <boolean>,
   "info": <string>,
   "ok": 1
}

The following is a prototype of the currentOp output when run on a primary of a replica set:

{
  "inprog": [
       {
         "host" : <string>,
         "desc" : <string>,
         "connectionId" : <number>,
         "client" : <string>,
         "appName" : <string>,
         "clientMetadata" : <document>,
         "active" : <boolean>,
         "currentOpTime" : <string>,
         "opid" : <number>,
         "secs_running" : <NumberLong()>,
         "microsecs_running" : <number>,
         "op" : <string>,
         "ns" : <string>,
         "command" : <document>,
         "originatingCommand" : <document>,
         "planSummary": <string>,
         "msg": <string>,
         "progress" : {
             "done" : <number>,
             "total" : <number>
         },
         "killPending" : <boolean>,
         "numYields" : <number>,
         "locks" : {
             "Global" : <string>,
             "MMAPV1Journal" : <string>,
             "Database" : <string>,
             "Collection" : <string>,
             "Metadata" : <string>,
             "oplog" : <string>
         },
         "waitingForLock" : <boolean>,
         "lockStats" : {
             "Global": {
                "acquireCount": {
                   "r": <NumberLong>,
                   "w": <NumberLong>,
                   "R": <NumberLong>,
                   "W": <NumberLong>
                },
                "acquireWaitCount": {
                   "r": <NumberLong>,
                   "w": <NumberLong>,
                   "R": <NumberLong>,
                   "W": <NumberLong>
                },
                "timeAcquiringMicros" : {
                   "r" : NumberLong(0),
                   "w" : NumberLong(0),
                   "R" : NumberLong(0),
                   "W" : NumberLong(0)
                },
                "deadlockCount" : {
                   "r" : NumberLong(0),
                   "w" : NumberLong(0),
                   "R" : NumberLong(0),
                   "W" : NumberLong(0)
                }
             },
             "MMAPV1Journal": {
                ...
             },
             "Database" : {
                ...
             },
             ...
         }
       },
       ...
   ],
   "fsyncLock": <boolean>,
   "info": <string>,
   "ok": <num>,
   "operationTime": <timestamp>,
   "$clusterTime": <document>
}

The following is a prototype of the currentOp output when run on a mongos of a sharded cluster:

{
   "inprog": [
        {
          "shard" : <string>,
          "host" : <string>,
          "desc" : <string>,
          "connectionId" : <number>,
          "client_s" : <string>,
          "appName" : <string>,
          "clientMetadata" : <document>,
          "active" : <boolean>,
          "currentOpTime": <string>,
          "opid" : <string>, // "<shard>:<opid>"
          "secs_running" : <NumberLong()>,
          "microsecs_running" : <number>,
          "op" : <string>,
          "ns" : <string>,
          "command" : <document>,
          "originatingCommand" : <document>,
          "planSummary": <string>,
          "msg": <string>,
          "progress" : {
              "done" : <number>,
              "total" : <number>
          },
          "killPending" : <boolean>,
          "numYields" : <number>,
          "locks" : {
              "Global" : <string>,
              "Database" : <string>,
              "Collection" : <string>,
              "Metadata" : <string>,
              "oplog" : <string>
          },
          "waitingForLock" : <boolean>,
          "lockStats" : {
              "Global": {
                 "acquireCount": {
                    "r": <NumberLong>,
                    "w": <NumberLong>,
                    "R": <NumberLong>,
                    "W": <NumberLong>
                 },
                 "acquireWaitCount": {
                    "r": <NumberLong>,
                    "w": <NumberLong>,
                    "R": <NumberLong>,
                    "W": <NumberLong>
                 },
                 "timeAcquiringMicros" : {
                    "r" : NumberLong(0),
                    "w" : NumberLong(0),
                    "R" : NumberLong(0),
                    "W" : NumberLong(0)
                 },
                 "deadlockCount" : {
                    "r" : NumberLong(0),
                    "w" : NumberLong(0),
                    "R" : NumberLong(0),
                    "W" : NumberLong(0)
                 }
              },
              "Database" : {
                 ...
              },
              ...
          }
        },
        ...
    ],
   "ok": <num>,
   "operationTime": <timestamp>,
   "$clusterTime": <document>
 }

Output Fields

Warning

极其谨慎地终止正在运行的操作。仅使用db.killOp()来终止由 Client 端启动的操作,而终止内部数据库操作。

在版本 3.0 中进行了更改:对于某些非活动的后台线程,例如非活动的signalProcessingThread,MongoDB 会抑制各种空字段。

仅在操作正在运行时显示;即activetrue

操作的持续时间(以微秒为单位)。 MongoDB 通过从操作的开始时间减去当前时间来计算该值。

仅在操作正在运行时显示;即activetrue

"query"操作包括读取操作。

"command"操作包括大多数commands,例如createIndexesfindandmodify

包含与此操作关联的完整命令对象的文档。如果命令文档超过 1 KB,则该文档具有以下格式:

"command" : {
  "$truncated": <string>,
  "comment": <string>
}

$truncated字段包含文档的字符串摘要,但不包括文档的comment字段(如果存在)。如果摘要仍超过 1 KB,则将其进一步截断,并在字符串末尾用省略号(...)表示。

如果向操作传递了 Comments,则comment字段存在。

以下示例输出包含对名为test的数据库中名为items的集合进行find操作的命令对象:

"command" : {
  "find" : "items",
  "filter" : {
    "sku" : 1403978
  },
  "$db" : "test"
}

以下示例输出包含由test的数据库中名为items的集合上的光标 ID 80336119321的命令所生成的getMore操作的命令对象:

"command" : {
  "getMore" : NumberLong("80336119321"),
  "collection" : "items",
  "$db" : "test"
}

运行该操作的 Client 端应用程序的标识符。如果操作在MongoDBShell中运行,则 appName 始终为"MongoDB Shell"。如果操作源自driver,则appName可以设置为自定义字符串。

locks文档报告了该操作当前持有的锁定的类型和模式。可能的锁定类型如下:

Lock Type Description
Global 代表全局锁定。
MMAPV1Journal 表示 MMAPv1 存储引擎特定的锁,用于同步日志写入;对于非 MMAPv1 存储引擎,MMAPV1Journal的模式为空。
Database 表示数据库锁定。
Collection 表示收集锁。
Metadata 表示元数据锁定。
oplog 表示锁定oplog

可能的模式如下:

Lock Mode Description
R 表示共享(S)锁。
W 表示排他(X)锁。
r 表示共享意图(IS)锁。
w 表示 Intent Exclusive(IX)锁。

通常,操作在需要访问 MongoDB 尚未完全读入内存的数据时产生。这样,在 MongoDB 读取数据进行 yield 操作的同时,内存中具有数据的其他操作即可快速完成。

仅在锁定时显示;即fsyncLocktrue

timeAcquiringMicros除以acquireWaitCount得出特定锁定模式的平均 await 时间。

首页