MongoDB 【已解决】mongoid 实现 group by 问题

liangbin661831 · January 22, 2015 · Last by liangbin661831 replied at January 22, 2015 · 8203 hits

我参考这篇文章:http://www.dixis.com/?p=531 clone 的 rubychina 的源码,打开 console,

Reply.collection.group(:key => 'user_id', :initial => { :count => 0 }, :reduce => "function(doc, prev){prev.count += 1;}")

没有反应啊,大神们这是怎么回事啊? mongodb 时启动的,rubychina 可以在本地跑起来。 或者大神门赐教 mongodb 怎么做 group by 比较好。

参考mongodb 文档, 里面对 map,reduce 等概念解释的很清楚,下面是我的代码:

map = %{ function(){emit(this.topic_id, this.id)}} " function(){emit(this.topic_id, this.id)}"

reduce = %{ function(key, values){return values.length} } " function(key, values){return values.length} "

Reply.map_reduce(map, reduce).out(inline: true).each {|d| d} [{"_id"=>1.0, "value"=>2.0}]

You need to Sign in before reply, if you don't have an account, please Sign up first.