Ruby MongoMapper中是否有方法实现与AR'类似的行为;s包括方法?

Ruby MongoMapper中是否有方法实现与AR'类似的行为;s包括方法?,ruby,mongomapper,eager-loading,Ruby,Mongomapper,Eager Loading,MongoMapper中是否有与此等效的功能: class Model < ActiveRecord::Base belongs_to :x scope :with_x, includes(:x) end 类模型

MongoMapper中是否有与此等效的功能:

class Model < ActiveRecord::Base
  belongs_to :x
  scope :with_x, includes(:x)
end
类模型
当运行Model.with_x时,这避免了对x的N次查询。
MongoMapper中是否有类似的功能?

当它是属于
关系的
时,您可以打开并运行两个查询,一个用于主文档,另一个用于所有关联文档。这是您所能做的最好的了,因为Mongo不支持连接

class Comment
  include MongoMapper::Document
  belongs_to :user
end

class User
  include MongoMapper::Document
  plugin MongoMapper::Plugins::IdentityMap
end

@comments = my_post.comments                # query 1
users = User.find(@comments.map(&:user_id)) # query 2

@comments.each do |comment|
  comment.user.name # user pulled from identity map, no query fired
end

(Mongoid有一个应用程序,但它的工作方式基本相同。)

此外,如果它是一个有很多对象的Rails应用程序,它可能会绑定到GC,因此快速加载将减少查询,但您的响应时间可能不会有多大改善。在我们的一个加载了约400个对象的应用程序上,这种急切的加载只为我们带来了约10%的请求响应时间改进。在请求期间禁用GC又为我们带来了35%。谢谢,这正是我想要的。