【问题标题】:Error on MongoId query with any_of and conditions on a same attribute在同一属性上使用 any_of 和条件进行 MongoId 查询时出错
【发布时间】:2020-12-16 13:24:56
【问题描述】:

运行以下 MongoId 查询时

Project.any_of(
  { completed_at: nil}, { :completed_at.gte => @start_date}
)

它会产生这个错误:

"此方法不准备处理 key 作为 Key 并且序列化器不是 nil"

我找到了这个here 的验证码,但我不明白它的含义。

使用.where( "$or" => [ 会产生同样的错误。

MongoId v7.1.5、Rails v6.0、MongoDB 4.4.x

【问题讨论】:

  • 试试where(completed_at: nil).or(:completed_at.gte => @start_date)
  • 没有涵盖此组合的测试用例,因此未实现。 @start_date 中有什么内容?
  • @start_date 是一个 Date 实例。此外,在此过滤条件之前,还有另一个带有 end_date 和适用于此属性的范围,但产生错误的较小查询是“Project.any_of ...”和我在上面发布的过滤器。跨度>
  • @Stefan 这似乎有效,谢谢!
  • @D.SM BTW,这个查询在一个非常旧的 mongoid 版本 3.1.4 上工作,在迁移到 v71.5 的过程中我看到了错误

标签: ruby mongodb mongoid querying


【解决方案1】:

这个的通用字符串是这样的:

criteria.any_of({ :field1 => "value" }, { :field2 => "value2" })

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多