Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:40:21+00:00 2026-06-01T11:40:21+00:00

So I have the following group by SQL select count(*) as NO_OF_MSGS,FROM_USER,PROFILE_IMG,MSG from MESSAGES

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:40:22+00:00Added an answer on June 1, 2026 at 11:40 am

    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.

    1. Run a M/R to get the count(*) grouped by X.
    2. Do a query on the M/R output with your sort(), skip(), limit().
    3. The above may not work if you have too much data to sort (~1M users), you may need to index the output of the M/R for the sort to work.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sql statement: select val, count(*) as tally from sometable group
I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following SQL statement: SELECT r.Result AS Resuly, COUNT(f.fieldID) AS [Count] FROM
I have the following sql query: SELECT field_number, COUNT(*) AS c FROM lead_detail WHERE
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have the following SQL:(bitemp) SELECT COUNT (*) AS Count FROM Table T WHERE
I have to perform the following SQL query: select answer_nbr, count(distinct user_nbr) from tpoll_answer
I have the following query: SELECT dev.DeviceName, Count(dom.DomainID) AS CountOfDomains FROM tblDevices dev JOIN
I have the following SQL: select Username, COUNT(DISTINCT Message) as Count, AVG(WordCount) as Average,
I have the following call in my RoR script: Foo.where(event = 'Login').group('user_id').order('user_id ASC').count() This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.