使用 MongoMapper 范围查询关联集合?
class Comment
include MongoMapper::Document
scope :by_rating, lambda { |minimum| where(:rating.gte => minimum) }
key :rating
belongs_to :user
end
class User
include MongoMapper::Document
many :comments
end
User.first.comments.by_rating(3)
最后一行的查询实际上做了什么? MongoMapper 是否足够智能,可以只执行一个具有两个 WHERE 条件(user_id 和最低评分)的查询?
class Comment
include MongoMapper::Document
scope :by_rating, lambda { |minimum| where(:rating.gte => minimum) }
key :rating
belongs_to :user
end
class User
include MongoMapper::Document
many :comments
end
User.first.comments.by_rating(3)
What does the query on last line actually do? Is MongoMapper intelligent enough to execute only one query with two WHERE conditions (user_id and minimum rating)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MongoDB 做不到这一点。这需要一个它无法做到的连接。它通过具有非常可扩展的读取性能和更轻量级的查询克服了这一限制。这不是问题。您可以通过在初始化程序中设置记录器(搜索 MongoMapper.connection)来查看此行为:
然后启动 Rails 控制台,您将看到两个查询:
MongoDB can't do that. That requires a join which it cannot do. It overcomes this limitation by having very scalable read performance and lighter-weight queries. It's not an issue. You can see this behavior by setting the logger in your initializer (search for MongoMapper.connection):
Then fire up your rails console and you'll see two queries: