开发者

How do I return only fields that are needed in Mongoid?

开发者 https://www.devze.com 2023-04-12 07:43 出处:网络
How do 开发者_StackOverflow中文版I perform a query that doesn\'t return the entire document, but only fields that I have specified?From the horse\'s mouth:

How do 开发者_StackOverflow中文版I perform a query that doesn't return the entire document, but only fields that I have specified?


From the horse's mouth:

# 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


you can also use pluck

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号