Postgres模式与所有JSONB字段上的类似/Ilike匹配
我的表格上有一个列,称为metadata
(例如trips
),该列是JSONB类型的。我想对元数据
的所有值进行对匹配/搜索。
例如,元数据
列的整个形式之一是:
{"city": "London", "trip_id": 2075209, "store_id": 108, "driver_id": 1166061, "driver_name": "Jon Doe", "track_back_order": false}
我想以伦敦的城市获取所有记录。我尝试将元数据列投入我的中的文本中,其中
子句
WHERE trips.metadata::TEXT ILIKE 'London'
返回0结果。
但是在字段级别上搜索有效,例如,子句返回适当的结果:
WHERE trips.metadata->>'city' ILIKE 'London'
但是,由于元数据列的键可以是任意的,因此我不能对每个字段进行上述查询。关于我如何解决这个问题的想法?
I have a column, called metadata
on my table (say trips
) which is of JSONB type. I'd like to pattern match/search on all values of metadata
.
For example, one of the entires of the metadata
column is of the form:
{"city": "London", "trip_id": 2075209, "store_id": 108, "driver_id": 1166061, "driver_name": "Jon Doe", "track_back_order": false}
I'd like to fetch all records with city as London. I've tried to cast the metadata column into text in my WHERE
clause with
WHERE trips.metadata::TEXT ILIKE 'London'
which returns 0 results.
But searching at a field level works, for example this WHERE
clause returns the appropriate results:
WHERE trips.metadata->>'city' ILIKE 'London'
But since the keys of the metadata column can be arbitrary, I cannot do the above query for each field. Any ideas on how I can solve for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ilike
或喜欢
没有任何通配符的与=
相同。因此 - 忽略案例灵敏度 -
trips.metadata :: text Ilike'伦敦'
基本上与trips.metadata :: text ='text ='伦敦'
显然没有工作。
如果您想在所有键上迭代:
ILIKE
orLIKE
without any wildcards is the same as=
.So - ignoring case sensitivity -
trips.metadata::TEXT ILIKE 'London'
is essentially the same astrips.metadata::TEXT = 'London'
Obviously that doesn't work.
You can use a JSON path expression if you want to iterate over all keys: