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 7971737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:42:02+00:00 2026-06-04T07:42:02+00:00

Possible Duplicate: How would I grab only articles with comments that were created 20

  • 0

Possible Duplicate:
How would I grab only articles with comments that were created 20 minutes ago?

Using mongodb and mongoid. How would one grab all articles and order by number of comments?

class Article
  include Mongoid::Document

  has_many :comments
end

class Comment
  include Mongoid::Document

  belongs_to :article
end
  • 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-04T07:42:03+00:00Added an answer on June 4, 2026 at 7:42 am

    I wasn’t sure on the direction you were intending to sort in, so I included indexes for both – you should delete one, if you’re not planning to use it, but this should do the trick for you:

    class Article
      include Mongoid::Document
    
      field :comments_count, :type => Integer, :default => 0
      index [[ :comments_count, Mongo::ASCENDING ]]
      index [[ :comments_count, Mongo::DESCENDING ]]
      has_many :comments
    
      before_save :update_comments_count
    
      protected
      def update_comments_count
        self.comments_count = self.comments.count
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What WPF books would you recommend? GUI's written using WPF seem to
Possible Duplicate: Why would one declare a Java interface method as abstract? I found
Possible Duplicate: How would that be possible to remove all event handlers of the
Possible Duplicate: Why would one declare a Java interface method as abstract? The following
Possible Duplicate: Detect Antivirus on Windows using C# In .net I would like to
Possible Duplicate: Why would one mark local variables and method parameters as “final” in
Possible Duplicate: jQuery/Javascript collision detection What would be the best way using preferably using
Possible Duplicate: How to execute an .SQL script file using c# How would I
Possible Duplicate: How would you compare jQuery objects? I'm using jQuery to store the
Possible Duplicate: Why would one mark local variables and method parameters as “final” in

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.