how to get connected clients in MongoDB

前端 未结 3 1987
误落风尘
误落风尘 2021-02-05 15:15

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

3条回答
  •  庸人自扰
    2021-02-05 16:01

    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 );
    }
    

提交回复
热议问题