การดู Connected Clients (การเชื่อมต่อปัจจุบัน) ใน MongoDB
ใช้ คำสั่ง db.currentOp(true) เพื่อดู Operations ที่กำลังทำงานอยู่ รวมถึง Client Connections ทั้งหมด
คำสั่งหลัก
> db.currentOp(true)
Parameter true: แสดง Operations ทั้งหมด (รวม Idle Connections) ไม่ใช่แค่ Active Operations
ผลลัพธ์ตัวอย่าง (สรุปสำคัญ)
{
"inprog" : [
{
"host" : "DESKTOP-QN2RB3H:27017",
"desc" : "conn1",
"connectionId" : 1,
"client" : "127.0.0.1:61787",
"appName" : "MongoDB Shell",
"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("da370b0f-093a-45f3-b1fe-697daca8ccd2") },
"secs_running" : NumberLong(0),
"microsecs_running" : NumberLong(160815),
"op" : "command",
"ns" : "admin.$cmd.aggregate",
"command" : { "currentOp" : 1, "$all" : true, "$db" : "admin" },
"numYields" : 0,
"locks" : {},
"waitingForLock" : false
},
{ "host" : "DESKTOP-QN2RB3H:27017", "desc" : "LogicalSessionCacheRefresh", "active" : false, ... },
{ "host" : "DESKTOP-QN2RB3H:27017", "desc" : "SessionKiller", "active" : false, ... },
{ "host" : "DESKTOP-QN2RB3H:27017", "desc" : "TTLMonitor", "active" : false, ... },
// ... Internal Threads
],
"ok" : 1
}
ฟิลด์สำคัญใน Output
| Field | คำอธิบาย |
|---|---|
| client | IP:Port ของ Client ที่เชื่อมต่อ (เช่น 127.0.0.1:61787) |
| appName | Application Name ที่ Client ส่งมา (Driver/Tool) |
| connectionId | Connection ID ภายใน MongoDB |
| desc | Description ของ Connection/Thread |
| active | true = กำลัง Execute Operation, false = Idle/Background Thread |
| op | Operation Type: query, command, insert, update, delete, getmore, killcursors |
| ns | Namespace (Database.Collection) ที่ Operation กำลังทำงาน |
| command | Command Document ที่กำลัง Execute |
| secs_running / microsecs_running | เวลาที่ Operation รันอยู่ (Detect Long-running Queries) |
| waitingForLock | true = รอ Lock (Potential Blocking) |
| clientMetadata | ข้อมูล Driver, OS, Application Version |