如何在 PostgreSQL 中使用 "LIKE" 查询 jsonb 列类型?

How do you use the "LIKE" query for jsonb column types in PostgreSQL?

对于 PostgreSQL 数据库中的 hstore 列,我知道我可以像 Ruby on Rails 中那样使用 "LIKE" 查询来搜索包含特定字符串的名称:

  Product.where("hstore_data -> 'author' LIKE '%billy%'")

我对 jsonb 列类型进行了尝试,但出现此错误:

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: jsonb ~~ unknown
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. : SELECT "products".* FROM "products" WHERE (jsonb_data -> 'author' LIKE '%billy%')

有没有办法为 jsonb 列类型正确使用 "LIKE"?

你可以试试这个

希望你有

product.jsonb_data = {
      author: "billynando"
   }

然后

Product.where("jsonb_data ->> :key LIKE :value",
  key: "author", value: "%billy%"
)

更多here

我知道答案已被接受,但 this Gist 可能会有所帮助。