简体   繁体   English

限制,计数,使用PHP在mongodb中与众不同

[英]limit,count,sort with distinct in mongodb using php

How can I use limit,count and sort with distinct in mongo from PHP ? 如何在PHP中使用mongo中的limit,count和sort进行排序? I am using mongo command functionality and the code is like : 我正在使用mongo命令功能,代码如下:

$data_count= $mongoInstance>command(array("distinct"=>"collection","key"=>"key","query"=>$filter_query));

How can I associate count, sort and limit with this ? 我该如何关联计数,排序和限制?

There is some aggeregate framework in mongo but how can I use it in PHP ? mongo中有一些aggeregate框架,但是如何在PHP中使用它呢?

I belive you are actually looking for the aggregation framework. 我相信您实际上正在寻找聚合框架。 The distinct command is quite limited in what it can do. distinct命令的功能非常有限。 An example of what you're looking for would be: 您要寻找的示例如下:

$db->col->aggregate(array(
    array('$match'=>$filter_query)
    array('$group'=>array('_id'=>'$key', 'count'=>array('$sum'=>1))),
    array('$sort'=>array('_id'=>-1)),
    array('$skip'=>4),
    array('$limit'=>20)
));

Further reference can be found here: http://docs.mongodb.org/manual/reference/aggregation/operator-nav/ 可以在以下位置找到更多参考: http : //docs.mongodb.org/manual/reference/aggregation/operator-nav/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM