I have a document with only about 200 elements in it. But I am still getting a very slow response just by querying it like:
Product.order_by([:name, :asc]).limit(20)
I do have an index on name which I have verified exists in the mongo shell. Is the only way to return efficiently sorted results to change they _id value to something like
"#{name}_#{random_id}"
Short answer: absolutely not.
Long answer: let’s make sure the query is using your index (not just that the index exists).
Our query:
This does not use an index (in this case, because it does not exist)
Let’s prove it
Create the index
Now when we explain the query: