How do I return only fields that are needed in Mongoid?
如何执行不返回整个文档,而仅返回我指定的字段的查询?
从马口中:
1 2 | # Return only the first and last names of each person. Person.only(:first_name, :last_name) |
来源:http://mongoid.org/docs/querying/criteria.html#only
您也可以使用
1 | Person.all.pluck(:first_name, :last_name, :id) |
http://www.rubydoc.info/github/mongoid/mongoid/Mongoid/Contextual/Mongo:pluck