Вопрос

I am trying to get an array of results from a Mongoid query similar to the following SQL;

select field1, field2 from table;  

The following returns a correct criteria, but I need the results

collection.only(:field1,:field2)

If I don't use the .only method, I can get results with .to_a:

collection.all.to_a

But if I add .to_a method along with the .only method:

collection.only(:field1,:field2).to_a 

it returns an error "(Object doesn't support #inspect)"

This seems pretty basic, am I missing something?

Interesting note, this syntax works on the Mongodb console;

db.collection.find({ }, { field1: 1, field2: 1 })

but how to do this from Mongoid?

Это было полезно?

Решение

i think you are looking for Criteria#pluck

collection.pluck(:field1,:field2)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top