currentOp

在本页面

Definition

  • currentOp
    • 返回一个document,其中包含有关mongod实例正在进行的操作的信息。

currentOp的格式如下:

{ currentOp: 1 }

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

Behavior

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

FieldDescription
"$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

  • currentOp. desc

  • currentOp. connectionId

    • 操作起源的连接的标识符。
  • currentOp. client

    • 一个字符串,其中包含有关操作起源的信息。
  • currentOp. appName

    • 一个字符串,其中包含有关发出请求的 Client 端类型的信息。
  • currentOp. opid

    • 操作的标识符。您可以将此值传递给mongo shell 中的db.killOp()来终止操作。

Warning

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

  • currentOp. active
    • 一个布尔值,指定操作是否已开始。如果操作已开始,则值为true;如果操作是空闲的,则值为false,例如空闲的连接或当前处于空闲状态的内部线程。即使一个操作已屈服于另一个操作,它也可以处于活动状态。

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

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

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

  • currentOp. microsecs_running
    • 2.6 版的新功能。

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

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

  • currentOp. op

    • 标识操作类型的字符串。可能的值为:
  • "none"

  • "update"

  • "insert"

  • "query"

  • "command"

  • "getmore"

  • "remove"

  • "killcursors"

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

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

  • currentOp. ns

  • currentOp. command

    • 在版本 3.6 中更改。

包含与此操作关联的完整命令对象的文档。如果命令文档超过 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"
}
  • currentOp. originatingCommand

    • 在版本 3.6 中进行了更改:对于用于从游标检索下一批结果的"getmore"操作,originatingCommand字段包含最初创建该游标的完整命令对象(例如findaggregate)。
  • currentOp. planSummary

    • 包含查询计划的字符串,可帮助调试缓慢的查询。
  • currentOp. client

    • 操作起源的 Client 端连接的 IP 地址(或主机名)和临时端口。如果您的inprog数组具有来自许多不同 Client 端的操作,请使用此字符串将操作与 Client 端相关联。
  • currentOp. appName

    • 3.4 版的新功能。

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

  • currentOp. locks
    • 在版本 3.0 中更改。

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

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

可能的模式如下:

Lock ModeDescription
R表示共享(S)锁。
W表示排他(X)锁。
r表示共享意图(IS)锁。
w表示 Intent Exclusive(IX)锁。
  • currentOp. waitingForLock

    • 返回一个布尔值。如果操作正在 await 锁定,则waitingForLocktrue,如果操作具有所需的锁定,则为false
  • currentOp. msg

    • msg提供一条消息,描述操作的状态和进度。对于索引或 mapReduce 操作,该字段报告完成百分比。
  • currentOp. progress

    • 报告有关 mapReduce 或索引操作的进度。 progress字段对应于msg字段中的完成百分比。 progress指定以下信息:
  • currentOp.progress. done

    • 报告完成的 Numbers。
  • currentOp.progress. total

    • 报告总数。
  • currentOp. killPending

    • 如果操作当前标记为终止,则返回true。当操作遇到下一个安全终止点时,操作将终止。
  • currentOp. numYields

    • numYields是一个计数器,它报告操作已屈服以允许其他操作完成的次数。

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

仅在锁定时显示;即fsyncLocktrue

  • currentOp. info

  • currentOp. lockStats

    • 对于每种锁类型和方式(有关锁类型和方式的说明,请参见currentOp.locks),返回以下信息:
  • currentOp.lockStats. acquireCount

    • 操作在指定模式下获得锁定的次数。
  • currentOp.lockStats. acquireWaitCount

  • currentOp.lockStats. timeAcquiringMicros

    • 操作必须 await 以获取锁的累积时间(以微秒为单位)。

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

  • currentOp.lockStats. deadlockCount
    • 操作在 await 锁获取时遇到死锁的次数。