資料庫呼叫
Mongoid 嘗試使用與 ActiveRecord
類似的語法。它支援這些呼叫(以及更多)
User.first #Gets first user from the database
User.count #Gets the count of all users from the database
User.find(params[:id]) #Returns the user with the id found in params[:id]
User.where(name: "Bob") #Returns a Mongoid::Criteria object that can be chained
#with other queries (like another 'where' or an 'any_in')
#Does NOT return any objects from database
User.where(name: "Bob").entries #Returns all objects with name "Bob" from database
User.where(:name.in => ['Bob', 'Alice']).entries #Returns all objects with name "Bob" or "Alice" from database
User.any_in(name: ["Bob", "Joe"]).first #Returns the first object with name "Bob" or "Joe"
User.where(:name => 'Bob').exists? # will return true if there is one or more users with name bob