RoR Tips & Tricks Series – #2

Tip : count is incorrect if used with limit in mongoid

Database : Mongodb

1.
campaigns = Campaign.all
campaigns.count
=> 211943

2.
campaigns = Campaign.limit(1000)
campaigns.count
=> 211943

campaigns[1001]
=> nil

I was expecting count as 1000 for second query, but it came out to be same as first one. But actual objects stored in campaigns array are 1000 as it gives nil for 1001st index.

I spend quite some time while writing program to figure out this. I don’t have answer to this. This works correct if your database is mysql or postgres.

Someone may give answer to this and someone will save time in figuring it out.

One thought on “RoR Tips & Tricks Series – #2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.