PostgreSQL jsonb 字符串格式
PostgreSQL jsonb string format
我正在使用 PostgreSQL jsonb 并且在我的数据库记录中有以下内容:
{"tags": "[\"apple\",\" orange\",\" pineapple\",\" fruits\"]",
"filename": "testname.jpg", "title_en": "d1", "title_ja": "1",
"description_en": "d1", "description_ja": "1"}
和两者 SELECT下面的语句检索没有结果:
SELECT "photo"."id", "photo"."datadoc", "photo"."created_timestamp","photo"."modified_timestamp"
FROM "photo"
WHERE datadoc @> '{"tags":> ["apple"]}';
SELECT "photo"."id", "photo"."datadoc", "photo"."created_timestamp", "photo"."modified_timestamp"
FROM "photo"
WHERE datadoc -> 'tags' ? 'apple';
不知是因为json数组字符串多加了反斜杠,还是SELECT语句不正确
我 运行 "PostgreSQL 10.1, compiled by Visual C++ build 1800, 64-bit" Windows 10.
PostgreSQL 文档是 here。
就任何 JSON 解析器而言,tags
键的值是一个字符串,而不是数组。
"tags": "[\"apple\",\" orange\",\" pineapple\",\" fruits\"]"
字符串本身恰好是另一个 JSON 文档,就像 XML 中的常见情况,其中字符串的内容恰好是 XML 或 HTML文档.
["apple"," orange"," pineapple"," fruits"]
您需要做的是提取该字符串,然后将其解析为新的 JSON 对象,然后查询该新对象。
我现在无法测试,但我认为它看起来像这样:
(datadoc ->> 'tags') ::jsonb ? 'apple'
也就是说,“将标签值提取为 text
,将 text
值转换为 jsonb
,然后查询新的 jsonb
值。
嘿,我知道这是一个很晚的答案,但这是一个很好的方法,我有数据。
数据库中的初始数据:
"{\"data\":{\"title\":\"test\",\"message\":\"string\",\"image\":\"string\"},\"registration_ids\":[\"s
tring\"],\"isAllUsersNotification\":false}"
将其转换为json
select (notificationData #>> '{}')::jsonb from sent_notification
结果:
{"data": {"image": "string", "title": "string", "message": "string"}, "registration_ids": ["string"], "isAllUsersNotification": false}
从json
获取数据object
select (notificationData #>> '{}' )::jsonb -> 'data' from sent_notification;
结果:
{"image": "string", "title": "string", "message": "string"}
从上面的结果中获取一个字段:
select (notificationData #>> '{}' )::jsonb -> 'data' ->>'title' from sent_notification;
结果:
string
执行 where 运算,
问:获取标题='string'
的记录
回答:
select * from sent_notification where (notificationData #>> '{}' )::jsonb -> 'data' ->>'title' ='string'
我正在使用 PostgreSQL jsonb 并且在我的数据库记录中有以下内容:
{"tags": "[\"apple\",\" orange\",\" pineapple\",\" fruits\"]",
"filename": "testname.jpg", "title_en": "d1", "title_ja": "1",
"description_en": "d1", "description_ja": "1"}
和两者 SELECT下面的语句检索没有结果:
SELECT "photo"."id", "photo"."datadoc", "photo"."created_timestamp","photo"."modified_timestamp"
FROM "photo"
WHERE datadoc @> '{"tags":> ["apple"]}';
SELECT "photo"."id", "photo"."datadoc", "photo"."created_timestamp", "photo"."modified_timestamp"
FROM "photo"
WHERE datadoc -> 'tags' ? 'apple';
不知是因为json数组字符串多加了反斜杠,还是SELECT语句不正确
我 运行 "PostgreSQL 10.1, compiled by Visual C++ build 1800, 64-bit" Windows 10.
PostgreSQL 文档是 here。
就任何 JSON 解析器而言,tags
键的值是一个字符串,而不是数组。
"tags": "[\"apple\",\" orange\",\" pineapple\",\" fruits\"]"
字符串本身恰好是另一个 JSON 文档,就像 XML 中的常见情况,其中字符串的内容恰好是 XML 或 HTML文档.
["apple"," orange"," pineapple"," fruits"]
您需要做的是提取该字符串,然后将其解析为新的 JSON 对象,然后查询该新对象。
我现在无法测试,但我认为它看起来像这样:
(datadoc ->> 'tags') ::jsonb ? 'apple'
也就是说,“将标签值提取为 text
,将 text
值转换为 jsonb
,然后查询新的 jsonb
值。
嘿,我知道这是一个很晚的答案,但这是一个很好的方法,我有数据。
数据库中的初始数据:
"{\"data\":{\"title\":\"test\",\"message\":\"string\",\"image\":\"string\"},\"registration_ids\":[\"s
tring\"],\"isAllUsersNotification\":false}"
将其转换为json
select (notificationData #>> '{}')::jsonb from sent_notification
结果:
{"data": {"image": "string", "title": "string", "message": "string"}, "registration_ids": ["string"], "isAllUsersNotification": false}
从json
获取数据objectselect (notificationData #>> '{}' )::jsonb -> 'data' from sent_notification;
结果:
{"image": "string", "title": "string", "message": "string"}
从上面的结果中获取一个字段:
select (notificationData #>> '{}' )::jsonb -> 'data' ->>'title' from sent_notification;
结果:
string
执行 where 运算,
问:获取标题='string'
的记录回答:
select * from sent_notification where (notificationData #>> '{}' )::jsonb -> 'data' ->>'title' ='string'