如何“分组依据”与MongoDB [英] How to "Group By" with MongoDB

查看:103
本文介绍了如何“分组依据”与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();

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/ query-builder-api.html


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();

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/query-builder-api.html

这篇关于如何“分组依据”与MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆