2

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,
                    "opid" : 101277,
                    "secs_running" : 2,
                    "microsecs_running" : NumberLong(2883686),
                    "op" : "getmore",
                    "ns" : "local.oplog.rs",
                    "query" : {
                            "getMore" : NumberLong("15696192304"),
                            "collection" : "oplog.rs",
                            "maxTimeMS" : NumberLong(5000),
                            "term" : NumberLong(117),
                            "lastKnownCommittedOpTime" : {
                                    "ts" : Timestamp(1501842736, 18),
                                    "t" : NumberLong(117)
                            }
                    },
                    "planSummary" : "COLLSCAN",
                    "numYields" : 0,
                    "locks" : {

                    },
                    "waitingForLock" : false,
                    "lockStats" : {
                            "Global" : {
                                    "acquireCount" : {
                                            "r" : NumberLong(2)
                                    }
                            },
                            "Database" : {
                                    "acquireCount" : {
                                            "r" : NumberLong(1)
                                    }
                            },
                            "oplog" : {
                                    "acquireCount" : {
                                            "r" : NumberLong(1)
                                    }
                            }
                    }
            },
....
...
..
3

You can try:

db.currentOp().inprog.forEach(function(x) { print(x.client) })
172.16.0.1:42768
172.16.0.4:59984
172.16.0.2:50643
172.16.0.1:20183

If you want to remove the port add a regex:

db.currentOp().inprog.filter(x => x.ns === 'local.oplog.rs').forEach(function(x) { print(x.client ? x.client.replace(/:.*$/,'') : '-') })
172.16.0.1
172.16.0.4
172.16.0.2
172.16.0.1
3

This will help to aggregate the information -

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)

  • What does the unknown mean? – troy Dec 6 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.