简体   繁体   English

如何在MongoDB PHP codeigniter中查询group by和distinct限制?

[英]how to query group by and distinct with a limit in MongoDB PHP codeigniter?

Is such a operation possible? 这样的手术可能吗?

sample record: 样本记录:

{ 
 _id: ObjectId("51d6be147483c58419000002"), 
 user: "ashok", 
 action: "login",
 time: 1373027860,
 details: { 
               user_entries:   "blah..blah", 
               url: "web.domain.com" 
                 }
 }

Suppose, i want to group by url visited, for each user, group by url where user = "ashok", limit 10. 假设,我想按访问的url分组,对于每个用户,按用户名=“ashok”的URL分组,限制10。

I am using AlexBilbie library for MongoDB-Codeigniter (it doesnt have aggregation). 我正在为MongoDB- Codeigniter使用AlexBilbie库(它没有聚合)。 so using plain php. 所以使用普通的PHP。
Still even if I could aggregate, how to distinct or limit it? 即使我可以聚合,如何区分或限制它?
Any suggestion is welcome. 任何建议都是受欢迎的。

First of all using group_by and distinct together doesn't make any sense. 首先使用group_by和distinct在一起没有任何意义。 Either you are using group_by or distinct. 您使用的是group_by或distinct。

If you want to do some kind of pagination for a grouped query you have to use map and reduce or the aggregation pipeline. 如果要对分组查询进行某种分页,则必须使用map和reduce或聚合管道。 In your case the aggregation would look like that. 在您的情况下,聚合看起来像那样。

db.users.aggregate(
  { '$match' => { 'user' => 'ashok' } },
  { '$group' => { '_id' => '$details.url' } },
  { '$skip'  => 0 },
  { '$limit' => 10 }
)

I am using this aggregation feature to display references at VersionEye . 我正在使用此聚合功能在VersionEye上显示引用 The aggregation feature allows to do grouping and paging on db level, that's why it's much faster then other ORM or pagination solutions. 聚合功能允许在数据库级别进行分组和分页,这就是为什么它比其他ORM或分页解决方案快得多。

在以下格式中,您可以给出限制:

$this->mongo_db->order_by(array('Student_Name'=>'asc'))->limit(20)->get('mycollection_name'); 

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

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