在Mongoid中,是否有一种类似于find_by_sql的查询方式,可以通过传递一个Mongo查询语句来从结果中实例化Mongoid::Document对象?
cursor = User.collection.find({}, {}) # Just like the Ruby driver...
records = cursor.to_a # An array of User objects
def each
@cursor.each do |document|
yield Mongoid::Factory.build(@klass, document)
end
end
db = Mongoid::Sessions.default
collection = db[:collection_name]
# finding a document
doc = collection.find(name: 'my new document').first
collection.find.each do |document|
puts document.inspect
end