【问题标题】:Python SQLAlchemy and Postgres - How to search using substring in json valuePython SQLAlchemy 和 Postgres - 如何使用 json 值中的子字符串进行搜索
【发布时间】:2020-03-22 03:52:41
【问题描述】:

我知道我可以使用此示例查询轻松查询 JSON 值:

r = Books.query.filter(
  Books.nameofjsonfield['key1', 'key2'].astext.cast(Unicode) == 'exact_string_to_compare'
).all()

但是如何搜索与 JSON 值的子字符串匹配的数据集?

【问题讨论】:

    标签: python json postgresql sqlalchemy


    【解决方案1】:

    将值转换为文本后,只需 using LIKE one way or the other 或任何其他模糊匹配方法即可:

    r = Books.query.\
        filter(Books.nameofjsonfield['key1', 'key2'].
               astext.
               cast(Unicode).
               contains('not_so_exact_string_to_find')).\
        all()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-12
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 2019-11-13
      相关资源
      最近更新 更多