json - 如何对 mongoDB 中的 db.currentOp() json 数组执行 group_by

标签 json mongodb bash shell

我需要编写一个 shell 脚本来生成格式化输出。

{
    "desc" : "conn98242",
    "threadId" : "140393378481956",
    "connectionId" : 98242,
    "client_s" : C1,
    "active" : true
 }

 {
  "desc" : "conn98249",
    "threadId" : "140393378481920",
    "connectionId" : 98249,
    "client_s" : C2,
    "active" : true
}
{
  "desc" : "conn98252",
    "threadId" : "140393378485620",
    "connectionId" : 98296,
    "client_s" : C1,
    "active" : true
}

这是 db.currentOp() 的输出。

我需要获得一个结果json文件,例如

{

"client_s" : C1
"count" : 2

}
{

"client_s" : C2
"count" : 1

}   

最佳答案

运行 db.aggregate() 助手 $currentOp on the admin database在第一阶段中,并在第二阶段中对该操作的结果进行分组:

use admin
db.aggregate([
   { $currentOp : { allUsers: true } },
   { $group : { 
       _id: '$client_s',
       count: { $sum: 1 }
   } }
])

或运行管理命令:

db.adminCommand({
   aggregate : 1,
   pipeline : [ 
        { $currentOp : { allUsers : true } }, 
        { $group : { 
            _id: '$client_s',
            count: { $sum: 1 }
        } }
    ],
    cursor : { }
})

关于json - 如何对 mongoDB 中的 db.currentOp() json 数组执行 group_by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51823380/

相关文章:

javascript - For 循环和顺序超时

python - 从特定网址抓取链接

java - 如何使用多个同名的 JSON 字段

node.js - TypeError : callback. apply 不是allowDiskUse之后的函数

bash - 打开由变量指定的文件描述符

linux - linux 的 IP 地址阻止列表

javascript - jQuery 触发器 - 仅返回单个对象,而不是通过 JSON 获取的所有对象

java - 将 MongoDB 条目转换为 Java 类模型对象

MongoDB 聚合 - $unwind 顺序文档是否与嵌套数组顺序相同

bash - 使用 find 删除所有子目录(及其文件)