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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:49:46+00:00 2026-06-11T11:49:46+00:00

This is an interesting pattern I found using mongoid (3.0.0), which I suspect is

  • 0

This is an interesting pattern I found using mongoid (3.0.0), which I suspect is a bug.

1.9.3p194 :007 > products = Product.order_by([:_id, :asc ]).limit(5)
 => #<Mongoid::Criteria
   selector: {},
   options:  {:sort=>{"_id"=>1}, :limit=>5},
   class:    Product,
   embedded: false>

1.9.3p194 :008 > products.map(&:_id)
 => ["500fa5614f6d3a23d0000002", "500fa5614f6d3a23d0000003", "500fa5614f6d3a23d0000004", "500fa5614f6d3a23d0000005", "500fa5614f6d3a23d0000006"] 

So far so good! However, if I issue the following – I get weird results.

1.9.3p194 :012 > products.count
 => 3654017 

This shows me all the product count instead of 5 (since i have :limit => 5)

1.9.3p194 :012 > Product.count
 => 3654017 

Now the even more weird part:

1.9.3p194 :010 > products.last
 => #<Product _id: 504952620a5e2323460000aa, _type: nil, ... >

This should have been _id: 500fa5614f6d3a23d0000006. Now, if I try to map the ids again, I get:

1.9.3p194 :019 > products.map(&:id)
 => ["504952620a5e2323460000aa", "504952620a5e2323460000a9", "504952620a5e2323460000a8", "5049524f0a5e2323460000a7", "504950ab0a5e2323460000a6"] 

This has changed the criteria entirely! However, I get proper results with this:

1.9.3p194 :008 > products = Product.order_by([:_id, :asc ]).limit(5)
 => #<Mongoid::Criteria
  selector: {},
  options:  {:sort=>{"_id"=>1}, :limit=>5},
  class:    Product,
  embedded: false>

1.9.3p194 :028 > products[0].id
 => "500fa5614f6d3a23d0000002" 
1.9.3p194 :029 > products[-1].id
 => "500fa5614f6d3a23d0000006" 

This does seem to be related to Mongoid 3.0.0 though. Any ideas?

  • 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-11T11:49:47+00:00Added an answer on June 11, 2026 at 11:49 am

    First keep in Mind that Mongoid has a lazy loading functionnality: the query will be trigerred at the last moment possible.

    Let’s dig your issues:

    • last: it sets limit to -1 so it will override your previous setting. To get your expected behavior, you must oblige Mongoid to make the query using to_a: products = Product.order_by([:_id, :asc ]).limit(5).to_a.last

    • count: if you want to respect the limit, use count(true)

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

Sidebar

Related Questions

I found this interesting behavior. Why could this happen I am using the cmdlet
i have been reading this interesting article which is increasing my every growing confusion
I found this interesting site, that helps me understand derivatives, http://www.numberempire.com/derivatives.php , It seems
In Matters Computational I found this interesting linear search implementation (it's actually my Java
For years I've been following a great pattern called Target-Action which goes like this:
I found this interesting blog post via CodingHorror: My Favorite Interview Question . In
Reading Real world Haskell i found some intresting question about data types: This pattern
Basing on this topic , I put down an interesting version of Singleton pattern
Hallo all. I found this interesting thread about how to invalidate a user session
This is an interesting concept which i couldn't figure out how to implement. (its

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.