[英]Is it possible to search a JSONB List for a value using "LIKE" and SqlAlchemy?
For example, if I have a JSONB column, and it is stored as:例如,如果我有一个 JSONB 列,它存储为:
["oranges","apples","bananas"]
How can I check using a LIKE type search if the substring "app" is in the list?如果子字符串“app”在列表中,如何使用 LIKE 类型搜索进行检查?
Ideally I would like be able to do something like:理想情况下,我希望能够执行以下操作:
Basket['fruits'].contains_like('%app%')
Is this possible with sqlalchemy?这可以用 sqlalchemy 实现吗?
Using inspiration from the pure PostgreSQL answer here , you can create the following condition:使用此处纯 PostgreSQL 答案的灵感,您可以创建以下条件:
from sqlalchemy import exists, func, literal_column
exists().select_from(
func.jsonb_array_elements_text(Basket['fruits']).alias('fruit')
).where(literal_column('fruit').like('%app%'))
The answer raises the issue of performance and potentially adding an index in special cases (when you're only interested in strings starting with a certain value).答案提出了性能问题,并可能在特殊情况下添加索引(当您只对以某个值开头的字符串感兴趣时)。 Have a look if that applies to you and if you experience performance issues看看这是否适用于您以及您是否遇到性能问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.