如何使用新的PostgreSQL JSON数据类型中的字段进行查询?
我正在寻找一些文档和/或示例在PostgreSQL 9.2中的新的JSON函数。
具体来说,给出一系列JSON记录: [ {name: "Toby",occupation: "Software Engineer"},{name: "Zaphod",occupation: "Galactic President"} ] 我如何写SQL来按名称查找记录? 在vanilla SQL中: SELECT * from json_data WHERE "name" = "Toby" 官方dev手册是相当稀疏: > http://www.postgresql.org/docs/devel/static/datatype-json.html 更新I 我把一个gist detailing what is currently possible with PostgreSQL 9.2。 SELECT id,json_string(data,'name') FROM things WHERE json_string(data,'name') LIKE 'G%'; 更新二 我现在把我的JSON函数移动到自己的项目: PostSQL – 一组用于将PostgreSQL和PL / v8转换为完全真棒的JSON文档存储的函数
Postgres 9.2
我引用Andrew Dunstan on the pgsql-hackers list:
不阻止他提供一个example implementation in PLV8应该解决你的问题。 Postgres 9.3 提供了一个新的功能和运算符添加“json处理”。 > The manual on new JSON functionality. 在Postgres 9.3中的原始问题的答案: SELECT * FROM json_array_elements( '[{"name": "Toby","occupation": "Software Engineer"},{"name": "Zaphod","occupation": "Galactic President"} ]' ) AS elem WHERE elem->>'name' = 'Toby'; 高级示例: > Query combinations with nested array of records in JSON datatype 对于较大的表,您可能需要添加一个表达式索引以提高性能: > Index for finding an element in a JSON array Postgres 9.4 添加jsonb(b为“二进制”,值作为本地Postgres类型存储),但是更多的功能为这两种类型。除了上面提到的表达式索引,jsonb还支持GIN,btree and hash indexes,GIN是最强大的。 > 手册最多建议:
大胆强调我。 Performance benefits from general improvements to GIN indexes. Postgres 9.5 完成jsonb函数和运算符。添加更多的函数来操纵jsonb到位和显示。 > Major good news in the release notes of Postgres 9.5. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |