-3

I have an array of ids. Now I want to get all the documents corresponding to the ids inside that array from a collection. Is there any command by which I can achieve this?

I don't want to run a loop over that array and query for every element of the array.

Assume the array is

id = [1,2,3,4]

The collection is Scores, which has the field id among other fields. I'm looking for something like Scores.find(..)

Swapnesh Khare
  • 109
  • 1
  • 10

1 Answers1

0

In ActiveRecord, the following query works as expected

Scores.find([1, 2, 3, 4])

Depending on the MongoDB adapter you use, it may work as well. According to this documentation, the same syntax is also supported in Mongoid.

Otherwise, you can generally use

Scores.where(id: [1, 2, 3, 4])

that will return a collection of records matching the given IDs.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364