如何“分组依据”与MongoDB [英] How to "Group By" with MongoDB
问题描述
我想知道是否有反正我可以用MongoDB / Php对结果进行分组。
这是我用来显示的代码我的行:
$ count = $ col-> find(array('order_id'=> array('$ gt '=>'0'))) - > count();
我需要按 order_id
对它们进行分组。
显然,我们不能在 group()上做一个
count()
code>作为 find()
作品: $ countfind = $ col - > find() - > count();
不工作: $ countgroup = $ ($ key) - > count();
我需要计算这个组。
感谢您的帮助。
有时,您可能想要获得一个数组集合中不同的值。您可以使用distinct()方法完成此操作:
$ ages = $ dm-> createQueryBuilder('User')$ b $>> distinct('age')
- > getQuery()
- > execute();
I would like to know if there is anyway I can "group by" my results with MongoDB/Php.
This is the code I'm using to display my rows:
$count = $col->find(array('order_id' => array('$gt' => '0')))->count();
I need to group them by order_id
.
Apparently, we cannot do a count()
on a group()
as find()
WORKS: $countfind = $col->find()->count();
DOESN'T WORK: $countgroup = $col->group($keys)->count();
I need to count this groupby please.
Thanks for your help.
Sometimes you may want to get an array of distinct values in a collection. You can accomplish this using the distinct() method:
$ages = $dm->createQueryBuilder('User')
->distinct('age')
->getQuery()
->execute();
这篇关于如何“分组依据”与MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!