Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get connected clients in MongoDB

I'm writing an app using mongo as its db. I want to print the clients connected to the db, for example, print their ip. How can I get that info?

I tried using

db.serverStatus().connections

But it gives me the number of computers with access to my db.

like image 825
itaied Avatar asked May 22 '14 13:05

itaied


People also ask

How can I get URI in MongoDB?

Find MongoDB URIClick on “Overview” tab in the menu bar. Scroll down the Overview page and you will see the MongoDB URI information.

Which command is used to see a connection in MongoDB?

The command connPoolStats returns information regarding the open outgoing connections from the current database instance to other members of the sharded cluster or replica set.

How many connections can MongoDB handle?

Your M2 cluster has three nodes with a 500 connection limit per node. Atlas reserves 10 connections per node. If you set your read preference to secondary, Atlas can read from the two secondary nodes for a combined 980 connection limit.


2 Answers

You should be able to run this command and get a list of connected IP addresses:

db.currentOp(true).inprog.forEach(function(d){if (d.client)printjson(d.client)})

db.currentOp is actually built on top of the special collection $cmd.sys.inprog so you can also query that directly. You can get an idea of the how to do that by typing in db.currentOp without the parentheses into the mongo shell and it will print out the source for the function:

> db.currentOp
function ( arg ){
    var q = {}
    if ( arg ) {
        if ( typeof( arg ) == "object" )
            Object.extend( q , arg );
        else if ( arg )
            q["$all"] = true;
    }
    return this.$cmd.sys.inprog.findOne( q );
}
like image 182
John Petrone Avatar answered Sep 28 '22 02:09

John Petrone


You can use db.currentOp(true) and iterate over the inprog array of the result set, using the client field.

like image 41
Markus W Mahlberg Avatar answered Sep 28 '22 04:09

Markus W Mahlberg