【问题标题】:Postgres query on nested JSONB field嵌套 JSONB 字段的 Postgres 查询
【发布时间】:2021-03-09 15:46:11
【问题描述】:

嘿,我有一个带有 JSONB 字段的表。我想运行一个查询(表扫描),它找到嵌套元素生命周期.time 等于“100”的所有记录。我"lifecycle:{time:"100"}

查看像 https://kb.objectrocket.com/postgresql/how-to-query-a-postgres-jsonb-column-1433 这样的文档没有显示查询 JSONB 字段的示例。

谢谢

【问题讨论】:

    标签: sql postgresql jsonb


    【解决方案1】:

    你没有提供太多细节,但据我所知,你想要这样的东西:

    select *
    from the_table
    where the_column -> 'lifecycle' ->> 'time' = '100'
    

    或者更短一点:

    select *
    from the_table
    where the_column @> '{"lifecycle": {"time": "100"}}';
    

    【讨论】:

    • 非常感谢。我想一个后续问题 - 如果我的嵌套元素是 "lifecycle:{time:"100", steps: [{id:1}, {id"2]} 并且我想找到包含生命周期.steps 的所有记录怎么办id 为 2 的元素?
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2023-04-03
    • 2019-03-14
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多