【问题标题】:How to make a query with AND condition in mongoid如何在 mongoid 中使用 AND 条件进行查询
【发布时间】:2012-06-08 18:08:13
【问题描述】:

如何在 Mongoid 中使用 AND 条件进行查询?

【问题讨论】:

  • 什么条件?等于 'x' 且大于 'y' ... ?

标签: ruby-on-rails ruby-on-rails-3 mongodb ruby-on-rails-3.1 mongoid


【解决方案1】:

您可以链接where 语句或在where 调用中包含多个条件。

Model.where(condition_a: :a, condition_b: :b).all

或

Model.where(condition_a: :a).where(condition_b: :b).all

【讨论】:

  • 根据您的条件,第二个where 可能会默默地覆盖第一个where 中的键,即where(foo: 'a').where(foo: 'b') 不等于where(foo: 'a').and(foo: 'b')
【解决方案2】:

另外,您可以使用.and(other_thing:'value') 链接事物

例如,Model.where(awesome:true).and(other_thing:true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 2015-09-25
    • 2011-12-31
    • 1970-01-01
    • 2017-09-09
    相关资源
    最近更新 更多