如何在文本字段rails postgresql中查询json数据

ove*_*low 6 ruby ruby-on-rails ruby-on-rails-3 rails-postgresql ruby-on-rails-4

我有一个数据库表column(collection)data type text

我已经插入了json数据 collection

这是我的json数据

{"name":"test","age":"25","country":"xxx"}
Run Code Online (Sandbox Code Playgroud)

但是现在我如何查询这个json数据.有以下选项吗?

User.where(collection: :name) # Here name denotes test
Run Code Online (Sandbox Code Playgroud)

Raj*_*jat 2

由于 JSON 是字符串,因此只能使用 SQLite/MySQL(或任何其他数据库)进行字符串比较。另外,尝试将数据类型保持为binary

您可以执行:LIKE操作以查找存储的 JSON 字符串中出现的子字符串。

您正在进行的上述比较将不起作用,因为它仅用于搜索集合列中的名称,而您已在该列中存储了整个 JSON 字符串。