How to return only those fields that are needed in Mongoid?

How to execute a query that does not return the entire document, but only the fields that I specified?

+5
source share
2 answers

From the mouth of a horse:

# Return only the first and last names of each person.
  Person.only(:first_name, :last_name)

Source: http://mongoid.org/docs/querying/criteria.html#only

+17
source

you can also use pluck

Person.all.pluck(:first_name, :last_name, :id)

http://www.rubydoc.info/github/mongoid/mongoid/Mongoid%2FContextual%2FMongo%3Apluck

+2
source

All Articles