Mongodb 获取已连接用户的列表

Mongodb 获取已连接用户的列表,mongodb,Mongodb,使用shell,我可以获得连接的用户列表吗?我可以使用db.serverStatus().connections获取连接数,还可以使用 db.currentOp(true).inprog.forEach(function(d){if (d.client)printjson(d.client)}) 但是,我想知道是否也可以从主机获取连接的用户?MongoDB 3.4.2 我一直希望找到一个类似MySQL的showProcessList的命令,以便轻松获取此类信息。令人失望的是,似乎没有任何功能

使用shell,我可以获得连接的用户列表吗?我可以使用db.serverStatus().connections获取连接数,还可以使用

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

但是,我想知道是否也可以从主机获取连接的用户?

MongoDB 3.4.2

我一直希望找到一个类似MySQL的
showProcessList
的命令,以便轻松获取此类信息。令人失望的是,似乎没有任何功能可以显示当前连接的用户,或者,对于给定的过去或当前连接,显示是哪个用户发起的连接。但是,启用了分析功能后,我们可以通过回顾最近的过去来大致了解其中的一些功能

在我的例子中,profiling全局设置为2(通过
db.getProfilingLevel()
可见),因此在每个数据库生命周期中的某个时间点,都会出现一个名为“system.profile”的集合,这是一个有上限的集合,即记录只会保留到集合已满,然后以先进先出的方式替换(请参阅下面的免责声明)。可以像往常一样查询此集合,并保留与每个操作相关联的时间戳(
ts

var all_dbs = db.runCommand({listDatabases:1});
var now = new Date();

// Modify the number of seconds for a larger or smaller time window
var just_now = new Date(now.getTime() - 1000); 

all_dbs.databases.forEach(
    function(current_db) {

        // The condition there is to exclude internal commands executed by the user "__system"
        // To exclude this command itself, add  the condition "ns:{$not:/profile$/}"
        all_operations = db.getSiblingDB(current_db.name).system.profile.find({user:{$not:/^__/}});
        all_operations.forEach(
            function(operation) {
                if (operation.ts > just_now) {

                    // Remove the .substring(0,200) to unlimit output length 
                    print(JSON.stringify(operation.ts),
                          JSON.stringify(operation.user),
                          JSON.stringify(operation.query||operation.command||operation.updateobj).substring(0,200));
}})});
输出如下所示:

"1998-04-01T22:14:56.431Z" "<user>@admin" "<database>.<collection>" "{ insert: \"<collection>\", ordered: false, documents: ...
“1998-04-01T22:14:56.431Z”“@admin”“{插入:\”\,顺序:false,文档:。。。
免责声明

  • 我确信这不是最好的方法,MongoDB实际上是我唯一使用Javascript的地方,所以我可能在这里做了一些事情,让真正的Javascript用户咬紧牙关

  • 对于具有大量活动的实例,这不起作用,而是引发此错误从system.profile返回结果,当它完成执行时,这些结果不再存在,因此会中断。
    查找命令期间的执行器错误:CappedPositionLost:Collections可能由于删除了capped collection中的位置而死亡。上次看到的记录id:RecordId(19041841)