【问题标题】:Complexe query with MongoID in rails在 Rails 中使用 MongoID 进行复杂查询
【发布时间】:2011-12-31 10:39:25
【问题描述】:

我的问题如下:

我有一个具有last_visited_atnotified_at 属性的Member 模型。

我想选择符合这些条件的对象:

( date_one last_visited_at 与 (notified_at OR date_two notified_at)

也可以写成:

date_one last_visited_at last_visited_at 与 (notified_at OR date_two notified_at )


这是我尝试过的,但没有成功:

我确保有一个对象符合这些条件。

(MongoID,使用 Rails 控制台)

Member.where(:last_visited_at => {"$lte" => date_one, "$gte" => date_two})
      .any_of(
              :notified_for_not_signing_in_at => {"$gte" => date_one},
              :notified_for_not_signing_in_at => {"$lte" => date_two})
      .first

返回错误

有人知道如何解决这个问题吗?

感谢您的帮助和时间,

【问题讨论】:

  • 错误是什么?此外,您的 any_of 哈希有两个相同键的条目,:notified_for_not_signing_in_at

标签: ruby-on-rails mongodb mongoid


【解决方案1】:

any_of 必须这样写(作为哈希)

   .any_of( {:notified_for_not_signing_in_at => {"$gte" => date_one}},
            {:notified_for_not_signing_in_at => {"$lte" => date_two}})

不是

any_of(
              :notified_for_not_signing_in_at => {"$gte" => date_one},
              :notified_for_not_signing_in_at => {"$lte" => date_two})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多