我需要编写一个 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/