相关疑难解决方法(0)

SQLAlchemy在PostgreSQL JSONB中过滤嵌套的JSON数据

我将嵌套JSON存储为jsonb,但我不知道如何选择具有不确定值的嵌套json.

例如:

{
    "facebook": {
        "openid": "123456789",
        "access_token": "6EFD26B0A868E3BB387E78851E42943F"
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道openid的值,但access_token是不确定的.

我尝试了以下但它引发了一个错误.

cls.query.filter(User.auth["facebook"]["openid"].astext == openid).first()
Run Code Online (Sandbox Code Playgroud)

python postgresql json sqlalchemy

5
推荐指数
1
解决办法
2517
查看次数

Postgres jsonb 查询动态值

在 users 表中,我有一个 jsob 列experience,其 json 结构如下:

[
    {
        "field": "devops",
        "years": 9
    },
    {
        "field": "backend dev",
        "years": 7
    } 
... // could be N number of objects with different values
]
Run Code Online (Sandbox Code Playgroud)

业务需求

客户可以要求在任何领域有经验的人员以及在每个领域各自有多年的经验

这是一个示例查询

SELECT * FROM users
WHERE
jsonb_path_exists(experience, '$[*] ? (@.field == "devops" && @.years > 5)') and
jsonb_path_exists(experience, '$[*] ? (@.field == "backend dev" && @.years > 5)')
LIMIT 3;
Run Code Online (Sandbox Code Playgroud)

问题

假设我收到请求

[
  { field: "devops", years: 5 }, 
  { field: "java", years: …
Run Code Online (Sandbox Code Playgroud)

sql postgresql node.js sequelize.js typeorm

3
推荐指数
1
解决办法
2050
查看次数

标签 统计

postgresql ×2

json ×1

node.js ×1

python ×1

sequelize.js ×1

sql ×1

sqlalchemy ×1

typeorm ×1