Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个使用 MongoDB 的 Rails 3 应用程序,其中 Mongoid 作为 ORM。我想查询集合中的特定字段。
为了查询我使用的特定集合的所有记录,User.all.to_a相当于User.all在 ActiveRecord 中。
User.all.to_a
User.all
现在我想查询一个集合中的所有记录,但只输出一个特定的字段。在这种情况下,我想查看所有用户名。我该怎么做呢?
我确定我已经在 Mongoid 文档中看到了这一点,只是遗漏了一些东西......
我在 mongoid 的新文档中找不到它,但这里有一个快速链接,只指向旧的 2.xx 文档。
基本上你需要做:
User.all.only(:name).to_a