我正在使用postgres数据库并尝试在此JSONB字段中查询“Value”=>“Black”的所有记录。该字段包含一组对象,例如{“id”=>“1”,“key”=>“尺寸”,“价值”=>“P”}
如何查询(不区分大小写)此记录?
这是我目前的代码
def by_feature_value(value)
relation.where('features @> ?', [{ value: value }].to_json)
end
记录
#<ProductSku:0x000055de9cc01ba8
id: 33,
product_id: 3,
code: "1234",
ean: "12345",
created_at: Mon, 30 Apr 2018 11:47:00 UTC +00:00,
updated_at: Mon, 30 Apr 2018 11:47:00 UTC +00:00,
features: [{"id"=>"2", "key"=>"Color", "Value"=>"Black"}]>
#<ProductSku:0x000055de9cc01ba8
id: 33,
product_id: 3,
code: "1234",
ean: "12345",
created_at: Mon, 30 Apr 2018 11:47:00 UTC +00:00,
updated_at: Mon, 30 Apr 2018 11:47:00 UTC +00:00,
features: [{"id"=>"2", "key"=>"Color", "Value"=>"black"}]>,
答案 0 :(得分:1)
对此的正确答案是
ProductSku.where('lower(features::text)::jsonb @> lower(?)::jsonb', [{ Value: value }].to_json)
如果没有类型转换,您帖子的第一条评论就不起作用。