如何在MongoDB中获得连接的客户端?

要在MongoDB中获得连接的客户端,请使用currentOp()设置值为true的方法,并且您需要在字段客户端的帮助下迭代数组结果集。让我们首先实现currentOp

> db.currentOp(true)

以下是输出。由于我们使用的是本地主机,因此客户端为127.0.0.1。输出显示所有连接的客户端

{
   "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"),
            "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
         },
         "secs_running" : NumberLong(0),
         "microsecs_running" : NumberLong(160815),
         "op" : "command",
         "ns" : "admin.$cmd.aggregate",
         "command" : {
            "currentOp" : 1,
            "$all" : true,
            "lsid" : {
               "id" : UUID("da370b0f-093a-45f3-b1fe-697daca8ccd2")
            },
            "$db" : "admin"
         },
         "numYields" : 0,
         "locks" : {
         },
         "waitingForLock" : false,
         "lockStats" : {
         }
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "LogicalSessionCacheRefresh",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "SessionKiller",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "TTLMonitor",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "free_mon",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "freemon-0",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "startPeriodicThreadToAbortExpiredTransactions",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "WTCheckpointThread",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "ftdc",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "WTJournalFlusher",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "clientcursormon",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      },
      {
         "host" : "DESKTOP-QN2RB3H:27017",
         "desc" : "initandlisten",
         "active" : false,
         "currentOpTime" : "2019-03-30T05:13:34.296+0530"
      }
   ],
   "ok" : 1
}