1

I'm currently use Mongoid to talk to my mongodb database. I have a query like the following:

ClassDoc.only(:class_id).where(:recurrs => true)

I'm only interested in the IDs as I exclude them in a subsequent query. However, the subsequent query needs the IDs in an array so I have to do this:

first_result_ids = ClassDoc.only(:class_id).where(:recurrs => true).collect { |class| class.class_id }

This takes quite a long time since AFAIK 'collect' loops over every result. It can take up to 10s to loop over just 1000 results.

Is there a way to pull the IDs out directly into an array?

1
  • If you only need class_id i would try to use an covered indexe for that query, so the result will come from the index only. this will speed up the queries a lot. see: docs.mongodb.org/manual/core/read-operations/… it may work with .only() in mongoid Commented Dec 12, 2012 at 10:16

1 Answer 1

1

try this

first_result_ids = ClassDoc.only(:class_id).where(:recurrs => true).map(&:class_id)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the try but no (or negligible) time difference between the two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.