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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:18:20+00:00 2026-05-22T22:18:20+00:00

I’m having an issue with ordering in Mongoid (2.0.0.beta.17 and 2.0.2)/MongoDB. Perhaps I’m just

  • 0

I’m having an issue with ordering in Mongoid (2.0.0.beta.17 and 2.0.2)/MongoDB. Perhaps I’m just not experienced enough with MongoDB, and some of you can help me understand what I’m doing wrong?

The user-level symptoms are:

  • Queries not sorting correctly by date or ID
  • User posts are not appearing when they seem like they should be (this is a Twitter-like social networking site for gardeners, and the most recent posts are sometimes not showing up)

irb(main):024:0> Update.all.size 

=> 551

Update.ordered.size     # (see below for definition) 

=> 490

irb(main):010:0> Update.all.select{|u| u.created_at.nil?} 

=> []

When I go into the mongo shell, and do:

var cursor = db.updates.find({}, {'_id': 1}).limit(600); 
while (cursor.hasNext()) printjson(cursor.next()); 

I get 454 lines returned.

var cursor = db.updates.find({}, {'_id': 1}).sort({created_at : 1}).limit(600); 
while (cursor.hasNext()) printjson(cursor.next());   

Also returns 454 lines.

db.updates.find({}).sort({created_at: -1}).limit(1); 

returns an update from February 23rd. But I have updates from yesterday in MongoDB.
Any ideas?


My model is:

class Update 
  include Mongoid::Document 
  include Mongoid::Timestamps 
  include Paperclip 
  field :body 
  ... 
  index [[ :created_at, Mongo::DESCENDING ]] 
  ... 
  named_scope :ordered, :order_by => ([[:created_at, :desc]]) 
  ... 
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-05-22T22:18:21+00:00Added an answer on May 22, 2026 at 10:18 pm

    I didn’t figure out WHY, but doing the following seems to fix the problem:

    named_scope :ordered, :order_by => ([[:created_at, :desc], [:_id, :desc]])

    It seems the secondary ordering helps.

    irb(main):022:0> update = Update.order_by([[:created_at, :desc], [:_id, :desc]])
    
    irb(main):025:0> update.count
    => 556
    irb(main):026:0> update.first.created_at
    => Sat May 28 02:53:33 -0700 2011
    irb(main):027:0> update.second.created_at
    => Fri May 27 17:55:38 -0700 2011
    irb(main):028:0> update.first.created_at.class
    => Time
    irb(main):029:0> update.second.created_at.class
    => Time
    

    The first of these was being left out of the original query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.