Reputation: 11903
So I have the following group by SQL
select count(*) as NO_OF_MSGS,FROM_USER,PROFILE_IMG,MSG from MESSAGES group by FROM_USER order by NO_OF_MSGS desc,DATE_SENT limit ?,?
How can I replicate this group by in MongoDB. I came across this excellent write up - http://kylebanker.com/blog/2009/11/mongodb-count-group/
This shows how you can use group by in Mongo, but it does not talk about how to implement order by
and limit
inside a group.
Also it seems Mongoid does not provide support for group
function, anyone know if its any different?
Upvotes: 0
Views: 2093
Reputation: 45277
How can I replicate this group by in MongoDB. I came across this excellent write up - http://kylebanker.com/blog/2009/11/mongodb-count-group/
First off, that is a 2-year old write-up. Those operators (count
, group
, distinct
) are still functional but they are quite slow. Using any of those operators amounts to running a Map/Reduce. And I'm not sure that sharding was ever implemented for those operators (note that the blog post pre-dates sharding).
The modern way to do this would be to use the new Aggregation Framework. This is both much faster and supports sharding. However, it is still in the unstable build.
This shows how you can use group by in Mongo, but it does not talk about how to implement order by and limit inside a group.
The query you are converting is a simple query in SQL, but this is not a simple query in MongoDB. The problem you are likely having with Mongoid is simply that you are doing something that MongoDB simply does not support (outside of the new Aggregation Framework).
If you do not have access to the Aggregation Framework, you will need to do this in multiple steps.
count(*) grouped by X
.sort()
, skip()
, limit()
.Upvotes: 1
Reputation: 5205
You should check out Mongoid: Querying.
It provides an explanation of how to use order_by
and limit
.
EDIT:
Removed comment about distinct
and group by
being equivalent as @mu pointed out. Instead, you should use group
and provide a reduce function as detailed in the article you linked to.
Upvotes: 0
Reputation: 1267
For grouping you can use group_by method of rails
http://api.rubyonrails.org/classes/Enumerable.html#method-i-group_by
And for ordering you can use sort_by method of Array
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-sort_by-21
Upvotes: 2