Giới thiệu
Để xem danh sách các client đang kết nối đến MongoDB server, bạn sử dụng lệnh db.currentOp(true) (với tham số true để hiển thị tất cả operations bao gồm idle connections).
Cú pháp
db.currentOp(true)
Kết quả mẫu
Lệnh trả về object chứa mảng inprog (in-progress operations). Mỗi phần tử đại diện cho một connection/operation:
{
"inprog" : [
{
"host" : "DESKTOP-QN2RB3H:27017",
"desc" : "conn1",
"connectionId" : 1,
"client" : "127.0.0.1:61787", // <-- Client IP:Port
"appName" : "MongoDB Shell", // Ứng dụng kết nối
"clientMetadata" : {
"application" : { "name" : "MongoDB Shell" },
"driver" : { "name" : "MongoDB Internal Client", "version" : "4.0.5" },
"os" : { "type" : "Windows", "name" : "Microsoft Windows 10", "architecture" : "x86_64", "version" : "10.0 (build 17134)" }
},
"active" : true,
"currentOpTime" : "2019-03-30T05:13:34.296+0530",
"opid" : 12538,
"lsid" : { "id" : UUID("..."), "uid" : BinData(...) },
"secs_running" : NumberLong(0),
"microsecs_running" : NumberLong(160815),
"op" : "command",
"ns" : "admin.$cmd.aggregate",
"command" : { "currentOp" : 1, "$all" : true, ... },
"numYields" : 0,
"locks" : {},
"waitingForLock" : false,
"lockStats" : {}
},
// Các background threads...
{ "host" : "...", "desc" : "LogicalSessionCacheRefresh", "active" : false, ... },
{ "host" : "...", "desc" : "SessionKiller", "active" : false, ... },
{ "host" : "...", "desc" : "TTLMonitor", "active" : false, ... },
...
],
"ok" : 1
}
Các trường quan trọng trong kết quả
| Trường | Ý nghĩa |
|---|---|
client | IP:Port của client kết nối (ví dụ: "127.0.0.1:61787") |
appName | Tên ứng dụng (do driver/client set) |
connectionId | ID connection nội bộ |
desc | Mô tả connection/thread |
active | true = đang thực thi operation, false = idle/background |
op | Loại operation: query, command, insert, update, delete, getmore... |
ns | Namespace: "database.collection" đang truy cập |
secs_running | Thời gian operation đã chạy (giây) |
clientMetadata | Thông tin driver, OS, application version |
Lọc chỉ client kết nối thực (loại background threads)
// Chỉ lấy connection từ client thực (có client IP)
db.currentOp(true).inprog.filter(op => op.client && op.client.includes(":"));
// Hoặc format đẹp
db.currentOp(true).inprog
.filter(op => op.client && op.client.includes(":"))
.forEach(op => print(`${op.client} | ${op.appName} | ${op.desc} | ${op.ns} | ${op.secs_running}s`));
Lệnh liên quan
| Lệnh | Chức năng |
|---|---|
db.currentOp() | Chỉ active operations (mặc định) |
db.currentOp(true) | Tất cả operations (kể cả idle) |
db.killOp(opid) | Kill operation theo opid |
db.runCommand({ connectionStatus: 1 }) | Xem info connection hiện tại |
db.serverStatus().connections | Thống kê connection (current, available, totalCreated) |
Trong MongoDB Atlas / Compass
- Atlas: Performance Advisor → Real-time Performance Panel
- Compass: Tab "Performance" → "Current Operations"
- Monitoring tools: Datadog, New Relic, Prometheus exporter
Lưu ý
- Cần quyền
inprogprivilege (roleclusterMonitorhoặchostManager) - Output có thể rất lớn trên server bận rộn - hãy filter
- Background threads (TTLMonitor, WTCheckpointThread...) không phải client thực