我想根据 MongoDB 中存储的数据实现一个直方图。我想根据分桶获得计数。我必须仅基于一个输入值(即组数)创建存储桶。例如 group = 4 考虑有多个事务正在运行,我们将事务时间存储为字段之一。我想根据完成交易所需的时间来计算交易计数。如何使用聚合框架或 MapReduce 来创建分桶?
Sample data:
{
"transactions": {
"149823": {
"timerequired": 5
},
"168243": {
"timerequired": 4
},
"168244": {
"timerequired": 10
},
"168257": {
"timerequired": 15
},
"168258": {
"timerequired": 8
},
"timerequired": 18
}
}
Run Code Online (Sandbox Code Playgroud)
在输出中,我想打印存储桶大小和属于该存储桶的交易数量。
桶数
0-5 2
5-10 2
10-15 1
15-20 1
mongodb ×1