Use only one query to count items in a collection #70

Open
wants to merge 1 commit into
from

Projects

None yet

1 participant

@trekdemo

Hi Guys,

Is there a reason for firing 3 queries to count items in a collection?

The previous version caused 3 queries:

  1. collection.empty? #=> load an item to ensure there are items in the collection
  2. collection.first ... #=> load an item again to be sure about we querying mongo
  3. collection.count(true) #=> actual count command
@trekdemo trekdemo With this approach mongoid will fire only one count query
The previous version caused 3 query:
1. collection.empty? => load an item to ensure there are items in the collection
2. collection.first ... => again load an item to be sure about we querying mongo
3. collection.count(true) => actual count command
3406353
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment