patternModerate
Mongodb list current connection
Viewed 0 times
listmongodbconnectioncurrent
Problem
I am using a db.currentOp() to check the current connection in Mongodb.
How can I get the total number of all record and how can I get all the "client" IP in a one column?
Thanks
```
db.currentOp()
{
"inprog" : [
{
"desc" : "WT RecordStoreThread: local.oplog.rs",
"threadId" : "1068",
"active" : true,
"opid" : 77300,
"secs_running" : 712,
"microsecs_running" : NumberLong(712734301),
"op" : "none",
"ns" : "local.oplog.rs",
"query" : {
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(1),
"w" : NumberLong(1)
}
},
"Database" : {
"acquireCount" : {
"w" : NumberLong(1)
}
},
"oplog" : {
"acquireCount" : {
"w" : NumberLong(1)
}
}
}
},
{
"desc" : "conn52",
"threadId" : "4692",
"connectionId" : 52,
"client" : "10.10.50.218:60909",
"active" : true,
How can I get the total number of all record and how can I get all the "client" IP in a one column?
Thanks
```
db.currentOp()
{
"inprog" : [
{
"desc" : "WT RecordStoreThread: local.oplog.rs",
"threadId" : "1068",
"active" : true,
"opid" : 77300,
"secs_running" : 712,
"microsecs_running" : NumberLong(712734301),
"op" : "none",
"ns" : "local.oplog.rs",
"query" : {
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(1),
"w" : NumberLong(1)
}
},
"Database" : {
"acquireCount" : {
"w" : NumberLong(1)
}
},
"oplog" : {
"acquireCount" : {
"w" : NumberLong(1)
}
}
}
},
{
"desc" : "conn52",
"threadId" : "4692",
"connectionId" : 52,
"client" : "10.10.50.218:60909",
"active" : true,
Solution
This will help to aggregate the information -
Connection Count by ClientIP, with Total
For Mongo Shell:
Formatted:
Example Output:
(the 192.x.x.x addresses are internal Atlas monitoring)
Connection Count by ClientIP, with Total
For Mongo Shell:
db.currentOp(true).inprog.reduce((accumulator, connection) => { ipaddress = connection.client ? connection.client.split(":")[0] : "unknown"; accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1; accumulator["TOTAL_CONNECTION_COUNT"]++; return accumulator; }, { TOTAL_CONNECTION_COUNT: 0 })Formatted:
db.currentOp(true).inprog.reduce(
(accumulator, connection) => {
ipaddress = connection.client ? connection.client.split(":")[0] : "unknown";
accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1;
accumulator["TOTAL_CONNECTION_COUNT"]++;
return accumulator;
},
{ TOTAL_CONNECTION_COUNT: 0 }
)Example Output:
{
"TOTAL_CONNECTION_COUNT" : 331,
"192.168.253.72" : 8,
"192.168.254.42" : 17,
"127.0.0.1" : 3,
"192.168.248.66" : 2,
"11.178.12.244" : 2,
"unknown" : 41,
"3.100.12.33" : 86,
"11.148.23.34" : 168,
"81.127.34.11" : 1,
"84.147.25.17" : 3
}(the 192.x.x.x addresses are internal Atlas monitoring)
Code Snippets
db.currentOp(true).inprog.reduce((accumulator, connection) => { ipaddress = connection.client ? connection.client.split(":")[0] : "unknown"; accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1; accumulator["TOTAL_CONNECTION_COUNT"]++; return accumulator; }, { TOTAL_CONNECTION_COUNT: 0 })db.currentOp(true).inprog.reduce(
(accumulator, connection) => {
ipaddress = connection.client ? connection.client.split(":")[0] : "unknown";
accumulator[ipaddress] = (accumulator[ipaddress] || 0) + 1;
accumulator["TOTAL_CONNECTION_COUNT"]++;
return accumulator;
},
{ TOTAL_CONNECTION_COUNT: 0 }
){
"TOTAL_CONNECTION_COUNT" : 331,
"192.168.253.72" : 8,
"192.168.254.42" : 17,
"127.0.0.1" : 3,
"192.168.248.66" : 2,
"11.178.12.244" : 2,
"unknown" : 41,
"3.100.12.33" : 86,
"11.148.23.34" : 168,
"81.127.34.11" : 1,
"84.147.25.17" : 3
}Context
StackExchange Database Administrators Q#182673, answer score: 11
Revisions (0)
No revisions yet.