我将嵌套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) 在 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)