问题描述
我正在尝试使用聚合框架将很多字符串分组在一起以标识唯一的字符串.我还必须保留有关其余字段的一些信息.这类似于我在mysql中使用*运算符并带有group by语句的情况.
I'm trying to use the aggregation framework to group a lot of strings together to indentify the unique ones. I must also keep some information about the rest of the fields. This would be analogous to me using the * operator in mysql with a group by statement.
SELECT *
FROM my_table
GROUP BY field1
我尝试使用聚合框架,并且它可以很好地用于获取唯一字段.
I have tried using the aggregation framework, and it works fine just to get unique fields.
db.mycollection.aggregate({
$group : { _id : "$field1"}
})
如果我想要随之而来的其他字段怎么办. MySQL只会给我第一个出现在小组中的人(我很好).那就是我认为$ first运算符所做的.
What if I want the other fields that went with that. MySQL would only give me the first one that appeared in the group (which I'm fine with). Thats what I thought the $first operator did.
db.mycollection.aggregate({
$group : {
_id : "$field1",
another_field : {$first : "$field2"}
}})
通过这种方式,它按field1进行分组,但仍给我附加到文档的其他字段.当我尝试这个时,我得到:
This way it groups by field1 but still gives me back the other fields attached to document. When I try this I get:
exception: aggregation result exceeds maximum document size (16MB)
我有一种感觉是因为它将整个聚合作为一个文档返回.我可以将其作为另一个json数组返回吗?
Which I have a feeling is because it is returning the whole aggregation back as one document. Can I return it as another json array?
预先感谢
推荐答案
您正在正确进行聚合,但是如错误消息所示,完整的不能大于16 MB.
You're doing the aggregation correctly, but as the error message indicates, the full result of the aggregate
call cannot be larger than 16 MB.
解决方法是添加一个过滤器以减小结果的大小,或者改用map-reduce并将结果输出到另一个集合.
Work-arounds would be to either add a filter to reduce the size of the result or use map-reduce instead and output the result to another collection.
这篇关于MongoDB,使用聚合分组框架来获取唯一的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!