gpt4 book ai didi

MongoDB - 我如何将这个 group() 查询转换为 map/reduce

转载 作者:可可西里 更新时间:2023-11-01 09:32:38 24 4
gpt4 key购买 nike

我有一个集合,其中每个文档看起来像这样

{access_key:'xxxxxxxxx', keyword: "banana", count:12, request_hour:"Thu Sep 30 2010 12:00:00 GMT+0000 (UTC)"}
{access_key:'yyyyyyyyy', keyword: "apple", count:25, request_hour:"Thu Sep 30 2010 12:00:00 GMT+0000 (UTC)", }
.....

要实现这一点:

SELECT keyword, sum(count) FROM keywords_counter WHERE access_key = 'xxxxxxxxx' GROUP BY 关键字

我这样做:

db.keywords_counter.group({key     : {keyword:true}, 
cond : {access_key: "xxxxx"},
reduce : function(obj, prev){prev.total += obj.count},
initial : {total:0}})

如何使用 map/reduce 实现相同的目的? [我是一个 map/reduce 初学者,正在努力思考这个概念。]

最佳答案

找到解决方案:

map = function(){ emit(this.keyword, {count: this.count}); }

reduce = function(key, values){
var total = 0;
for (var i=0; i < values.length, i++) { total += values[i].count; }
return {count: total};
}

db.keywords_counter.mapReduce(map, reduce, {query:{access_key: 'xxxxxxxxx'}})

关于MongoDB - 我如何将这个 group() 查询转换为 map/reduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3854033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com