db.currentOp()
定义
db.currentOp()
返回一份文档,其中包含有关数据库实例正在进行的操作的信息。
db.currentOp()
方法封装了数据库命令currentOp
。使用
$currentOp
聚合阶段,而不是currentOp
命令及其mongosh
辅助方法db.currentOp()
。注意
从 MongoDB 5.0 开始,在运行带有
mongosh
的辅助方法db.currentOp()
时使用$currentOp
聚合阶段。鉴于此,在 5.0 版本的 shell 以及使用 mongosh 时,
db.currentOp()
结果集不受先前旧版mongo
的 16 MB BSON 文档返回大小限制的约束。
语法
db.currentOp()
采用以下形式:
db.currentOp(<operations>)
db.currentOp()
可以采用以下可选参数:
行为
db.currentOp()
可以接受筛选器文档或布尔参数。
如果向 db.currentOp()
传递过滤器文档,输出将只返回与过滤器匹配的当前操作的信息。过滤器文档可以包含:
字段 | 说明 |
---|---|
"$ownOps" | 布尔值。如果设为 在 |
"$all" | 布尔值。如果设置为 如果文件中包含 |
<filter> |
将 true
传递给 db.currentOp()
等同于传入 { "$all": true }
文档。以下操作具有同等效果:
db.currentOp(true) db.currentOp( { "$all": true } )
db.currentOp()
和数据库分析器会为所有 CRUD 操作报告相同的基本诊断信息,其中包括以下内容:
getMore
(OP_GET_MORE 和command
)
这些操作也包含在慢查询的日志中。有关慢查询日志的更多信息,请参阅 slowOpThresholdMs
。
访问控制
在使用 authorization
运行的系统上,用户必须具有包含 inprog
特权动作的访问权限。
即使没有 inprog
特权动作,用户也可以在 mongod
实例上运行 db.currentOp( { "$ownOps": true }
)
,来查看自己的操作。
示例
以下示例使用 db.currentOp()
方法和各种查询文档来过滤输出结果。
等待锁定的写入操作
以下示例返回有关等待锁的所有写操作的信息:
db.currentOp( { "waitingForLock" : true, $or: [ { "op" : { "$in" : [ "insert", "update", "remove" ] } }, { "command.findandmodify": { $exists: true } } ] } )
无产出的活跃操作
以下示例将返回有关从未出现的所有正在运行的活动操作的信息:
db.currentOp( { "active" : true, "numYields" : 0, "waitingForLock" : false } )
针对特定数据库的活动操作
以下示例将返回有关运行时间超过 3 秒且针对数据库 db1
的所有活动操作的信息:
db.currentOp( { "active" : true, "secs_running" : { "$gt" : 3 }, "ns" : /^db1\./ } )
主动索引操作
以下示例返回索引创建操作的相关信息:
db.adminCommand( { currentOp: true, $or: [ { op: "command", "command.createIndexes": { $exists: true } }, { op: "none", "msg" : /^Index Build/ } ] } )
输出示例
以下是 db.currentOp()
输出的原型。
以下是在独立实例上运行时 currentOp
输出的原型:
{ "inprog": [ { "type" : <string>, "host" : <string>, "desc" : <string>, "connectionId" : <number>, "client" : <string>, "appName" : <string>, "clientMetadata" : <document>, "active" : <boolean>, "currentOpTime" : <string>, "effectiveUsers" : [ { "user" : <string>, "db" : <string> } ], "opid" : <number>, "lsid" : { "id" : <UUID>, "uid" : <BinData> }, "secs_running" : <NumberLong()>, "microsecs_running" : <number>, "op" : <string>, "ns" : <string>, "command" : <document>, "queryFramework" : <string>, "planSummary": <string>, "cursor" : { // only for getMore operations "cursorId" : <NumberLong()>, "createdDate" : <ISODate()>, "lastAccessDate" : <ISODate()>, "nDocsReturned" : <NumberLong()>, "nBatchesReturned" : <NumberLong()>, "noCursorTimeout" : <boolean>, "tailable" : <boolean>, "awaitData" : <boolean>, "originatingCommand" : <document>, "planSummary" : <string>, "operationUsingCursorId" : <NumberLong()> }, "msg": <string>, "progress" : { "done" : <number>, "total" : <number> }, "killPending" : <boolean>, "numYields" : <number>, "dataThroughputLastSecond" : <number>, "dataThroughputAverage" : <number>, "waitingForLatch" : { "timestamp" : <ISODate()>, "captureName" : <string> }, "locks" : { "ParallelBatchWriterMode" : <string>, "ReplicationStateTransition" : <string>, "Global" : <string>, "Database" : <string>, "Collection" : <string>, "Metadata" : <string>, "oplog" : <string> }, "waitingForLock" : <boolean>, "lockStats" : { "ParallelBatchWriterMode" : { "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) } }, "ReplicationStateTransition" : { ... }, "Global": { ... }, "Database" : { ... }, ... } }, ... ], "fsyncLock": <boolean>, "info": <string>, "ok": <num> }
以下是在副本集的主节点上运行时 currentOp
输出的原型:
{ "inprog": [ { "type" : <string>, "host" : <string>, "desc" : <string>, "connectionId" : <number>, "client" : <string>, "appName" : <string>, "clientMetadata" : <document>, "lsid" : { "id" : <UUID>, "uid" : <BinData> }, "transaction" : { "parameters" : { "txnNumber" : <NumberLong()>, "autocommit" : <boolean>, "readConcern" : { "level" : <string> } }, "readTimestamp" : <Timestamp>, "startWallClockTime" : <string>, "timeOpenMicros" : <NumberLong()>, "timeActiveMicros" : <NumberLong()>, "timeInactiveMicros" : <NumberLong()>, "expiryTime" : <string>, }, "active" : <boolean>, "currentOpTime" : <string>, "effectiveUsers" : [ { "user" : <string>, "db" : <string> } ], "opid" : <number>, "secs_running" : <NumberLong()>, "microsecs_running" : <number>, "op" : <string>, "ns" : <string>, "command" : <document>, "originatingCommand" : <document>, "queryFramework" : <string>, "planSummary": <string>, "prepareReadConflicts" : <NumberLong()>, "writeConflicts" : <NumberLong()>, "cursor" : { // only for getMore operations "cursorId" : <NumberLong()>, "createdDate" : <ISODate()>, "lastAccessDate" : <ISODate()>, "nDocsReturned" : <NumberLong()>, "nBatchesReturned" : <NumberLong()>, "noCursorTimeout" : <boolean>, "tailable" : <boolean>, "awaitData" : <boolean>, "originatingCommand" : <document>, "planSummary" : <string>, "operationUsingCursorId" : <NumberLong()> }, "msg": <string>, "progress" : { "done" : <number>, "total" : <number> }, "killPending" : <boolean>, "numYields" : <number>, "dataThroughputLastSecond" : <number>, "dataThroughputAverage" : <number>, "waitingForLatch" : { "timestamp" : <ISODate()>, "captureName" : <string> }, "locks" : { "ParallelBatchWriterMode" : <string>, "ReplicationStateTransition" : <string>, "Global" : <string>, "Database" : <string>, "Collection" : <string>, "Metadata" : <string>, "oplog" : <string> }, "waitingForLock" : <boolean>, "lockStats" : { "ParallelBatchWriterMode" : { "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) } }, "ReplicationStateTransition" : { ... }, "Global" : { ... }, "Database" : { ... }, ... } }, ... ], "fsyncLock": <boolean>, "info": <string>, "ok": <num>, "operationTime": <timestamp>, "$clusterTime": <document> }
下面是在分片集群的 mongos
上运行时的 currentOp
输出示例(字段可能因报告的操作而异):
{ "inprog": [ { "shard": <string>, "type" : <string>, "host" : <string>, "desc" : <string>, "connectionId" : <number>, "client_s" : <string>, "appName" : <string>, "clientMetadata" : <document>, "lsid" : { "id" : <UUID>, "uid" : <BinData> }, "transaction" : { "parameters" : { "txnNumber" : <NumberLong()>, "autocommit" : <boolean>, "readConcern" : { "level" : <string> } }, "readTimestamp" : <Timestamp>, "startWallClockTime" : <string>, "timeOpenMicros" : <NumberLong()>, "timeActiveMicros" : <NumberLong()>, "timeInactiveMicros" : <NumberLong()>, "expiryTime" : <string>, }, "active" : <boolean>, "currentOpTime" : <string>, "effectiveUsers" : [ { "user" : <string>, "db" : <string> } ], "runBy" : [ { "user" : <string>, "db" : <string> } ], "twoPhaseCommitCoordinator" : { "lsid" : { "id" : <UUID>, "uid" : <BinData> }, "txnNumber" : <NumberLong>, "numParticipants" : <NumberLong>, "state" : <string>, "commitStartTime" : <ISODate>, "hasRecoveredFromFailover" : <boolean>, "stepDurations" : <document>, "decision" : <document>, "deadline" : <ISODate> } "opid" : <string>, "secs_running" : <NumberLong()>, "microsecs_running" : <number>, "op" : <string>, "ns" : <string>, "command" : <document>, "configTime" : <Timestamp>, // Starting in 5.0 "topologyTime" : <Timestamp>, // Starting in 5.0 "queryFramework" : <string>, // Starting in 6.2 "planSummary": <string>, "prepareReadConflicts" : <NumberLong()>, "writeConflicts" : <NumberLong()>, "cursor" : { // only for getMore operations "cursorId" : <NumberLong()>, "createdDate" : <ISODate()>, "lastAccessDate" : <ISODate()>, "nDocsReturned" : <NumberLong()>, "nBatchesReturned" : <NumberLong()>, "noCursorTimeout" : <boolean>, "tailable" : <boolean>, "awaitData" : <boolean>, "originatingCommand" : <document>, "planSummary" : <string>, "operationUsingCursorId" : <NumberLong()> }, "msg": <string>, "progress" : { "done" : <number>, "total" : <number> }, "killPending" : <boolean>, "numYields" : <number>, "dataThroughputLastSecond" : <number>, "dataThroughputAverage" : <number>, "waitingForLatch" : { "timestamp" : <ISODate()>, "captureName" : <string> }, "locks" : { "ParallelBatchWriterMode" : <string>, "ReplicationStateTransition" : <string>, "Global" : <string>, "Database" : <string>, "Collection" : <string>, "Metadata" : <string>, "oplog" : <string> }, "waitingForLock" : <boolean>, "lockStats" : { "ParallelBatchWriterMode": { "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) } }, "ReplicationStateTransition" : { ... }, "Global" : { ... }, "Database" : { ... }, ... } }, ... ], "ok": <num>, "operationTime": <timestamp>, "$clusterTime": <document> }
特定输出示例
这些输出示例说明了特定操作的 currentOp
输出。构成实际输出的字段因服务器角色而异。
重新分片输出示例
{ type: "op", desc: "Resharding{Donor, Recipient, Coordinator}Service <reshardingUUID>", op: "command", ns: "<database>.<collection>", originatingCommand: { reshardCollection: "<database>.<collection>", key: <shardkey>, unique: <boolean>, collation: {locale: "simple"}, // Other options to the reshardCollection command are omitted // to decrease the likelihood the output is truncated. }, {donor, coordinator, recipient}State : "<service state>", approxDocumentsToCopy: NumberLong(<count>), approxBytesToCopy: NumberLong(<count>), bytesCopied: NumberLong(<count>), countWritesToStashCollections: NumberLong(<count>), countWritesDuringCriticalSection : NumberLong(<count>), countReadsDuringCriticalSection: NumberLong(<count>), deletesApplied: NumberLong(<count>), documentsCopied: NumberLong(<count>), insertsApplied: NumberLong(<count>), oplogEntriesFetched: NumberLong(<count>), oplogEntriesApplied: NumberLong(<count>), remainingOperationTimeEstimatedSecs: NumberLong(<count>), allShardsLowestRemainingOperationTimeEstimatedSecs: NumberLong(<estimate>), allShardsHighestRemainingOperationTimeEstimatedSecs: NumberLong(<estimate>), totalApplyTimeElapsedSecs: NumberLong(<count>), totalCopyTimeElapsedSecs: NumberLong(<count>), totalCriticalSectionTimeElapsedSecs : NumberLong(<count>), totalOperationTimeElapsedSecs: NumberLong(<count>), updatesApplied: NumberLong(<count>), }
全局索引示例
{ type: "op", desc: "GlobalIndex{Donor, Recipient, Coordinator}Service <globalIndexUUID}", op: "command", ns: "<database>.<collection>", originatingCommand: { createIndexes: "<database>.<collection>", key: <indexkeypattern>, unique: <boolean>, <Additional createIndexes options> }, {donor, coordinator, recipient}State : "<service state>", approxDocumentsToScan: NumberLong(<count>), approxBytesToScan: NumberLong(<count>), bytesWrittenFromScan: NumberLong(<count>), countWritesToStashCollections: NumberLong(<count>), countWritesDuringCriticalSection : NumberLong(<count>), countReadsDuringCriticalSection: NumberLong(<count>), keysWrittenFromScan: NumberLong(<count>), remainingOperationTimeEstimatedSecs: NumberLong(<count>), allShardsLowestRemainingOperationTimeEstimatedSecs: NumberLong(<estimate>), allShardsHighestRemainingOperationTimeEstimatedSecs: NumberLong(<estimate>), totalCopyTimeElapsedSecs: NumberLong(<count>), totalCriticalSectionTimeElapsedSecs : NumberLong(<count>), totalOperationTimeElapsedSecs: NumberLong(<count>), }
输出字段
有关 db.currentOp()
输出字段的完整列表,请参阅 currentOp。