对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

db.currentOp()(mongosh方法)

db.currentOp()

重要

自版本 6.2 以来,currentOp数据库命令已弃用。使用 $currentOp 聚合阶段而不是 currentOp 命令及其 mongosh 辅助方法 db.currentOp()。

返回一份文档,其中包含有关数据库实例正在进行的操作的信息。db.currentOp() 方法封装了数据库命令 currentOp。

注意

从 MongoDB 5.0 开始,在运行带有 mongosh 的辅助方法 db.currentOp() 时使用$currentOp 聚合阶段。

鉴于此,在 5.0 版本的 shell 以及使用 mongosh 时,db.currentOp() 结果集不受先前旧版 mongo 的 16 MB BSON 文档返回大小限制的约束。

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

重要

M0 和 Flex 集群不支持此命令。有关更多信息,请参阅不支持的命令。

db.currentOp() 采用以下形式:

db.currentOp(<operations>)

db.currentOp() 可以采用以下可选参数:

Parameter
类型
说明

布尔或文档

可选。指定要报告的操作。可以传递布尔值或文档:

  • 指定 true 以包括针对空闲连接的操作和系统操作。

  • 指定带有查询条件的文档,以仅报告符合这些条件的操作。详情请参阅行为。

db.currentOp() 可以接受筛选器文档或布尔参数。

如果向 db.currentOp() 传递过滤器文档,输出将只返回与过滤器匹配的当前操作的信息。过滤器文档可以包含:

字段
说明

"$ownOps"

布尔值。如果设为 true,则只返回有关当前用户的操作的信息。

在 mongod 实例上,用户可以随时运行 db.currentOp( { "$ownOps": true } ) 以查看自己的操作。

"$all"

布尔值。如果设置为 true,则返回所有操作的信息,包括对空闲连接的操作和系统操作。有关更多信息,请参阅 $currentOp(聚合阶段)。

如果文件中包含 "$all": true 和输出字段条件,则只适用 "$all":true。

<filter>

指定输出字段的过滤条件。请参阅示例。

如果文件中包含 "$all": true 和输出字段条件,则只适用 "$all": true。

将 true 传递给 db.currentOp() 等同于传入 { "$all": true } 文档。以下操作具有同等效果:

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

db.currentOp() 和数据库分析器会为所有 CRUD 操作报告相同的基本诊断信息,其中包括以下内容:

这些操作也包含在慢查询的日志中。有关慢查询日志的更多信息,请参阅 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.getSiblingDB("admin").aggregate( [ { $currentOp : { idleConnections: true } }, { $match: { $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" : <Long()>, "microsecs_running" : <number>, "op" : <string>, "ns" : <string>, "command" : <document>, "queryFramework" : <string>, "planSummary": <string>, "cursor" : { // only for getMore operations "cursorId" : <Long()>, "createdDate" : <ISODate()>, "lastAccessDate" : <ISODate()>, "nDocsReturned" : <Long()>, "nBatchesReturned" : <Long()>, "noCursorTimeout" : <boolean>, "tailable" : <boolean>, "awaitData" : <boolean>, "originatingCommand" : <document>, "planSummary" : <string>, "operationUsingCursorId" : <Long()> }, "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" : Long(0), "w" : Long(0), "R" : Long(0), "W" : Long(0) }, "deadlockCount" : { "r" : Long(0), "w" : Long(0), "R" : Long(0), "W" : Long(0) } }, "ReplicationStateTransition" : { ... }, "Global": { ... }, "Database" : { ... }, ... } }, ... ], "fsyncLock": <boolean>, "info": <string>, "ok": <num>}

这些输出示例说明了特定操作的 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: Long(<count>), approxBytesToScan: Long(<count>), bytesWrittenFromScan: Long(<count>), countWritesToStashCollections: Long(<count>), countWritesDuringCriticalSection : Long(<count>), countReadsDuringCriticalSection: Long(<count>), keysWrittenFromScan: Long(<count>), remainingOperationTimeEstimatedSecs: Long(<count>), allShardsLowestRemainingOperationTimeEstimatedSecs: Long(<estimate>), allShardsHighestRemainingOperationTimeEstimatedSecs: Long(<estimate>), totalCopyTimeElapsedSecs: Long(<count>), totalCriticalSectionTimeElapsedSecs : Long(<count>), totalOperationTimeElapsedSecs: Long(<count>),}

有关 db.currentOp() 输出字段的完整列表,请参阅 currentOp。