find_by_sql等价于蒙古包吗? [英] find_by_sql equivalent for mongoid?
本文介绍了find_by_sql等价于蒙古包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否存在与mongoid等效的find_by_sql,您在其中传递了mongo查询,并从结果中实现了Mongoid :: Document?
Is there some sort of find_by_sql equivalent for mongoid, where you pass a mongo query and it materializes Mongoid::Document s from the results?
推荐答案
Mongoid包装Collection对象以返回适当类的对象.
Mongoid wraps the Collection object to return objects of the proper class.
因此,如果用户是Mongoid模型:
So, if User is a Mongoid model:
cursor = User.collection.find({}, {}) # Just like the Ruby driver...
records = cursor.to_a # An array of User objects
编辑添加:它实际上也包装了Mongo的Cursor类. 请参见此处:
Edit to add: It actually wraps Mongo's Cursor class too. See here:
def each
@cursor.each do |document|
yield Mongoid::Factory.build(@klass, document)
end
end
这篇关于find_by_sql等价于蒙古包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文