【发布时间】:2023-04-04 14:15:01
【问题描述】:
我在 MongoDB 中有一个日志集合,其结构如下所示:
{
url : "http://example.com",
query : "name=blah,;another_param=bleh",
count : 5
}
其中“查询”字段是请求网址中的查询参数。 我想计算按查询参数“名称”分组的总数。例如,对于这个集合:
[{
url : "http://example.com",
query : "name=blah,;another_param=bleh",
count : 3
},
{
url : "http://example.com",
query : "name=blah,;another_param=xyz",
count : 4
},
{
url : "http://example.com",
query : "name=another_name,;another_param=bleh",
count : 3
}]
我需要这个输出:
[{
key : "blah",
count : 7
},
{
key : "another_name",
count : 3
}]
看起来我无法使用聚合框架进行此字符串操作。我可以通过 map-reduce 做到这一点,但是 map-reduce 操作可以成为聚合管道的一部分吗?
【问题讨论】:
标签: javascript mongodb mapreduce mongodb-query aggregation-framework