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.
Find MongoDB URIClick on “Overview” tab in the menu bar. Scroll down the Overview page and you will see the MongoDB URI information.
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.
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.
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 );
}
You can use db.currentOp(true)
and iterate over the inprog
array of the result set, using the client
field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With