在 MongoDB 中,
db.currentOp() 输出过长时,可以通过过滤操作、精简字段、排除无关内容等方式优化,使其更易读。以下是具体方法:1. 过滤关键操作(按条件筛选)
默认输出包含所有操作(包括内部心跳、空闲连接等),可通过查询条件过滤出需要关注的操作(如长时间运行的操作、特定类型的操作等)。
1.1:只看运行超过 x 秒/微秒的操作
// 筛选运行时间 > 5秒的操作(排除瞬间完成的)
db.currentOp({ secs_running: { $gt: 5 } })
只输出部分字段:
db.currentOp().inprog.filter(op => op.secs_running> 5).map(op => ({ opid: op.opid, op: op.op, ns: op.ns, command: op.command })).forEach(op => printjson(op));
// 筛选运行时间 > 100微秒的操作(排除瞬间完成的) -- 1000 微妙 = 1 毫秒
db.currentOp({ microsecs_running: { $gt: 5000 } })
只输出部分字段:
db.currentOp().inprog.filter(op => op.microsecs_running> 5000).map(op => ({ opid: op.opid, op: op.op, ns: op.ns, command: op.command })).forEach(op => printjson(op));
1.2:只看写操作(update/insert/delete)
// 筛选操作类型为 update/insert/delete 的操作
db.currentOp({
op: { $in: ["update", "insert", "delete"] }
})
1.3:排除内部心跳命令(hello/ismaster)
你的输出中大量是
hello 或 ismaster 命令(驱动的集群检测心跳),可过滤掉:// 排除 hello/ismaster 命令(这些是正常的集群检测,无需关注)
db.currentOp({ $and: [ { op: "command" }, { "command.hello": { $exists: false } }, // 排除 hello 命令 { "command.isMaster": { $exists: false } }, // 排除 isMaster命令 { "command.ismaster": { $exists: false } } // 排除 ismaster 命令 ] })
1.4:统计操作总数
use admin // 确保在admin数据库
db.aggregate([
// 第一步:获取所有当前操作(可加基础过滤,如排除空闲连接)
{ $currentOp: { allUsers: true, idleConnections: false } },
// 第二步:筛选运行时间>500微秒的操作(替代filter)
{ $match: { microsecs_running: { $gt: 500 } } },
// 第三步:统计数量
{ $count: "op_count" }
]);
1.5:按照不同语句类型统计
1. 统计该 IP 的查询操作(op: "query")且时长 > 100 毫秒的总数
方法 1:用 db.currentOp + 正则匹配
// 筛选条件:IP为10.20.9.82(任意端口) + 查询操作 + 时长>100毫秒
var queryOps = db.currentOp({
client: /^10.20.9.82:/, // 正则匹配IP(忽略端口)
op: "query",
microsecs_running: { $gt: 100000 } // 100毫秒 = 100,000微秒
});
print("IP 10.20.9.82 的慢查询总数:", queryOps.inprog.length);
方法 2:用聚合管道(在 admin 库执行)
use admin;
db.aggregate([
{ $currentOp: { allUsers: true, idleConnections: false } },
{ $match: {
client: { $regex: /^10.20.9.82:/ }, // 正则匹配IP
op: "query",
microsecs_running: { $gt: 100000 }
}},
{ $count: "ip_10_20_9_82_query_count" }
]);
2. 统计该 IP 的更新操作(op: "update")且时长 > 100 毫秒的总数
方法 1:用 db.currentOp + 正则匹配
// 筛选条件:IP为10.20.9.82(任意端口) + 更新操作 + 时长>100毫秒
var updateOps = db.currentOp({
client: /^10.20.9.82:/, // 正则匹配IP
op: "update",
microsecs_running: { $gt: 100000 }
});
print("IP 10.20.9.82 的慢更新总数:", updateOps.inprog.length);
方法 2:用聚合管道(在 admin 库执行)
use admin;
db.aggregate([
{ $currentOp: { allUsers: true, idleConnections: false } },
{ $match: {
client: { $regex: /^10.20.9.82:/ }, // 正则匹配IP
op: "update",
microsecs_running: { $gt: 100000 }
}},
{ $count: "ip_10_20_9_82_update_count" }
]);
2. 精简字段(只显示关键信息)
默认输出包含大量细节(如锁信息、集群时间等),可通过投影(projection) 只保留核心字段(如操作 ID、类型、集合、运行时间、客户端等)。
2.1 只显示关键字段
db.currentOp(
{ secs_running: { $gt: 3 } }, // 过滤条件:运行超3秒
{
opid: 1, // 操作ID(用于终止操作)
op: 1, // 操作类型(query/update/insert等)
ns: 1, // 操作的集合(db.collection)
secs_running: 1, // 运行时间(秒)
client: 1, // 客户端IP和端口
"command.q": 1, // 查询条件(如果是查询/更新)
_id: 0 // 隐藏默认的_id字段
}
)
输出会简化为类似:
{
"inprog": [
{
"opid": -1866306343,
"op": "command",
"ns": "admin.$cmd",
"secs_running": NumberLong(7),
"client": "10.20.9.132:52262"
},
...
],
"ok": 1
}
3. 使用聚合管道更灵活处理
通过
$currentOp 聚合阶段,可结合 $match(过滤)、$project(精简)、$sort(排序)等更灵活地处理。3.1 筛选运行超 1 毫秒的写操作,并按运行时间排序
use admin;
db.aggregate([
// 第一步:获取当前操作(排除空闲连接)
{ $currentOp: { allUsers: true, idleConnections: false } },
// 第二步:过滤条件(运行超5秒的写操作)
{ $match: {
microsecs_running: { $gt: 1000 },
op: { $in: ["update", "insert", "delete"] }
}},
// 第三步:只保留关键字段
{ $project: {
opid: 1,
op: 1,
ns: 1,
microsecs_running: 1,
client: 1,
"command.q": 1
}},
// 第四步:按运行时间倒序(最长的在前)
{ $sort: { secs_running: -1 } }
])
3.2 筛选运行超 1 毫秒的查询,并按运行时间排序
use admin;
db.aggregate([
{ $currentOp: { allUsers: true, idleConnections: false } },
{ $match: {
microsecs_running: { $gt: 1000 },
op: { $in: ["update", "insert", "delete"] }
}},
{ $project: {
opid: 1,
op: 1,
ns: 1,
microsecs_running: 1,
client: 1,
"command.q": 1
}},
{ $sort: { secs_running: -1 } }
])
3.3 筛选运行超 1 毫秒的所有操作,并按运行时间排序
use admin;
db.aggregate([
{ $currentOp: { allUsers: true, idleConnections: false } },
{ $match: {
microsecs_running: { $gt: 1000 }
}},
{ $project: {
opid: 1,
op: 1,
ns: 1,
microsecs_running: 1,
client: 1,
"command.q": 1
}},
{ $sort: { secs_running: -1 } }
])
3.4 筛选运行超 1 毫秒的所有操作,去除系统操作
use admin;
db.aggregate([
{ $currentOp: { allUsers: true, idleConnections: false } },
{ $match:
{ $and:
[
{ microsecs_running: { $gt: 1000 } },
{ "command.hello": { $exists: false } },
{ "command.isMaster": { $exists: false } },
{ "command.ismaster": { $exists: false } }
]
}
},
{ $project: { opid: 1, op: 1, ns: 1, microsecs_running: 1, client: 1, "command": 1 }},
{ $sort: { secs_running: -1 } }
])
4. 排除空闲连接和内部线程
默认可能包含空闲连接或内部线程(如
WTCheckpointThread),可通过参数排除:// 排除空闲连接和内部系统线程
db.currentOp({
idleConnections: false, // 排除空闲连接
$or: [
{ type: "op" }, // 只保留用户操作
{ desc: { $not: /(WTCheckpointThread|JournalFlusher)/ } } // 排除内部线程
]
})
5. 按照DB、文档查询
5.1 按DB查询语句
在 MongoDB 中,若需查询
ase-szss 数据库下的当前操作,并仅输出指定字段,需通过 db.currentOp() 筛选命名空间(匹配库名)+ 投影指定字段 实现。以下是完整查询语句及说明:// 查询 ase-szss 库下的当前操作,仅输出指定字段(排除默认的 _id 字段)
扩展:筛选「慢操作」(可选)
若需仅查询
若需仅查询
ase-szss 库下的慢操作(如运行超 100 毫秒),可在筛选条件中增加时间阈值:use admin db.aggregate([ { $currentOp: { allUsers: true, // 查看所有用户的操作 idleConnections: false // 不包括空闲连接 } }, { $match: { // 设置筛选条件 100毫秒、admin开头的库 "microsecs_running": { "$gt": 100000 }, "ns": /^ase-sess\./ // 替换 your_db_name 为你的数据库名,只查看该库的操作 } }, { $project: { // 指定返回的字段,1表示包含,0表示排除 "opid": 1, "op": 1, "ns": 1, "secs_running": 1, "client": 1, "command": 1, "planSummary": 1, "locks": 1, "waitingForLock": 1, "_id": 0 // 通常不需要 _id } } ])
5.2 按文档名查询语句
在 MongoDB 中,按「数据库(ase)+ 集合(product)」精准筛选当前操作,需通过 命名空间(ns)精确匹配(格式:
数据库名.集合名),同时保留指定输出字段。以下是完整查询语句及说明:// 精准筛选 ase 库下 product 集合的当前操作,仅输出指定字段
use admin db.aggregate([ { $currentOp: { allUsers: true, // 查看所有用户的操作 idleConnections: false // 不包括空闲连接 } }, { $match: { // 设置筛选条件 100毫秒、admin开头的库 "microsecs_running": { "$gt": 100000 }, "ns": /^ase\.product/ // 替换 your_db_name 为你的数据库名,只查看该库的操作 } }, { $project: { // 指定返回的字段,1表示包含,0表示排除 "opid": 1, "op": 1, "ns": 1, "secs_running": 1, "client": 1, "command": 1, "planSummary": 1, "locks": 1, "waitingForLock": 1, "_id": 0 // 通常不需要 _id } } ])
6. 总结
核心思路是:先过滤掉无关操作(如心跳、瞬间完成的操作),再精简字段只保留核心信息。根据你的输出,主要需要排除
hello/ismaster 命令(集群心跳),并关注长时间运行的实际业务操作(如 update)。根据需求组合上述方法,即可大幅简化输出。
posted on
浙公网安备 33010602011771号