首页 > 解决方案 > 使用 Rails 5.1.6 查询 PostgresSQL(9.6.5) JSONb 列对于多个键值对

问题描述

我的数据库中有一个名为 events 的列。

t.jsonb "events"

在那里我存储了一个有哈希的数组。

[{ type: 'bounce', bounce_class: '25'}...]

我可以通过使用这个where语句找到带有键的哈希记录:类型的值对:'bounce' 和bounce_class:'25' 。

where("events @> ?", [{type: "bounce", bounce_class: '25'}].to_json)

我想不通的是如何在退回类键中查找多个值:值对而不链接一堆这样的语句。

where("events @> ?", [{type: "bounce", bounce_class: '25'}].to_json).or(where("events @> ?", [{type: "bounce", bounce_class: '26'}].to_json))

我想找到所有类型为“bounce”并且bounce_class 为“25”或“26”或“50”的记录。

有没有比链接语句更好的方法?

谢谢

标签: postgresql-9.6ruby-on-rails-5.1

解决方案


推荐阅读