MongoDB 按功能分组
在MySQL中,
select a,b,count(1) as cnt from list group by a, b having cnt > 2;
我必须使用mongodb中的具有条件来执行按函数分组。 但我收到以下错误。请分享您的意见。
在 MongoDB 中
> res = db.list.group({key:{a:true,b:true},
... reduce: function(obj,prev) {prev.count++;},
... initial: {count:0}}).limit(10);
Sat Jan 7 16:36:30 uncaught exception: group command failed: {
"errmsg" : "exception: group() can't handle more than 20000 unique keys",
"code" : 10043,
"ok" : 0
一旦执行,我们需要在下一步运行以下文件。
for (i in res) {if (res[i].count>2) printjson(res[i])};
问候, 库马兰
In MySQL
select a,b,count(1) as cnt from list group by a, b having cnt > 2;
I have to execute the group by function using having condition in mongodb.
But i am getting following error. Please share your input.
In MongoDB
> res = db.list.group({key:{a:true,b:true},
... reduce: function(obj,prev) {prev.count++;},
... initial: {count:0}}).limit(10);
Sat Jan 7 16:36:30 uncaught exception: group command failed: {
"errmsg" : "exception: group() can't handle more than 20000 unique keys",
"code" : 10043,
"ok" : 0
Once it will be executed, we need to run the following file on next.
for (i in res) {if (res[i].count>2) printjson(res[i])};
Regards,
Kumaran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的组的数量,您可能会通过使用 distinct 找到比 group 或 MapReduce 更简单、更快的解决方案:
如果你在 a 和 b 上有索引
Depends on the number of your groups, you might find a simpler and faster solution than group or MapReduce by using distinct:
It will be faster if you have indexes on a and b
MongoDB group by 在大多数情况下是非常有限的,例如,
所以最好使用 Map Reduce。所以查询就像这样
map = function() { emmit({a:true,b:true},{count:1}); }然后
它是一个未经测试的版本。让我知道它是否有效
编辑:
早期的地图功能有故障。这就是为什么你没有得到结果。它应该是
测试数据:
编辑2:
完整的解决方案,包括应用计数> = 2
MongoDB group by is very limited in most cases, for instance
So its better to use map reduce. so the query would be like this
map = function() { emit({a:true,b:true},{count:1}); }and then
Its a untested version. let me know if it works
EDIT:
The earlier map function was faulty. Thats why you are not getting the results. it should have been
Test data:
EDIT2:
Complete solution including applying having count >= 2
您应该使用 MapReduce 代替。团体有其局限性。
将来您将能够使用聚合框架。但现在,使用map/reduce。
You should use MapReduce instead. Group has its limitations.
In future you'll be able to use the Aggregation Framework. But for now, use map/reduce.