jsonb

Postgres jsonb search in array with greater operator (with jsonb_array_elements)

落爺英雄遲暮 提交于 2020-01-02 12:20:13
问题 I try to search a solution but I didn't find anything for my case... Here is the database declaration (simplified): CREATE TABLE documents ( document_id int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY, data_block jsonb NULL ); And this is an example of insert. INSERT INTO documents (document_id, data_block) VALUES(878979, {"COMMONS": {"DATE": {"value": "2017-03-11"}}, "PAYABLE_INVOICE_LINES": [ {"AMOUNT": {"value": 52408.53}}, {"AMOUNT": {"value": 654.23}} ]}); INSERT INTO documents (document

How to push a JSON object to a nested array in a JSONB column

假如想象 提交于 2020-01-01 10:28:09
问题 I need to somehow push a JSON object to a nested array of potentionally existing JSON objects - see "pages" in the below JSON snippet. { "session_id": "someuuid", "visitor_ui": 1, "pages": [ { "datetime": "2016-08-13T19:45:40.259Z", "duration,": 0, "device_id": 1, "url": { "path": "/" } }, { "datetime": "2016-08-14T19:45:40.259Z", "duration,": 0, "device_id": 1, "url": { "path": "/test" } }, // how can i push a new value (page) here?? ] "visit_page_count": 2 } I'm aware of the jsonb_set

How to push a JSON object to a nested array in a JSONB column

妖精的绣舞 提交于 2020-01-01 10:28:07
问题 I need to somehow push a JSON object to a nested array of potentionally existing JSON objects - see "pages" in the below JSON snippet. { "session_id": "someuuid", "visitor_ui": 1, "pages": [ { "datetime": "2016-08-13T19:45:40.259Z", "duration,": 0, "device_id": 1, "url": { "path": "/" } }, { "datetime": "2016-08-14T19:45:40.259Z", "duration,": 0, "device_id": 1, "url": { "path": "/test" } }, // how can i push a new value (page) here?? ] "visit_page_count": 2 } I'm aware of the jsonb_set

How to select only part of json, stored in Postgres, with ActiveRecord

邮差的信 提交于 2020-01-01 10:08:33
问题 Say I have a model User , which has a field of type json called settings . Let's assume that this field looks roughly like this: { color: 'red', language: 'English', subitems: { item1: true, item2: 43, item3: ['foo', 'bar', 'baz'] } } If I do User.select(:settings) I will get all the settings for each user. But I want to get only the languages for a user. I tried both: User.select("settings -> 'language'") and User.select("settings ->> 'language'") but this just returns empty objects: [#<User

Does JSONB make PostgreSQL arrays useless?

被刻印的时光 ゝ 提交于 2020-01-01 04:19:32
问题 Suppose that you want to store "tags" on your object (say, a post). With release 9.4 you have 3 main choices: tags as text[] tags as jsonb tags as text (and you store a JSON string as text) In many cases, 3rd would be out of question since it wouldn't allow query conditional to 'tags' value. In my current development, I don't need such queries, tags are only there to be shown on posts list, not to filter posts. So, choice is mostly between text[] and jsonb . Both can be queried. What would

Appending (pushing) and removing from a JSON array in PostgreSQL 9.2, 9.3, and 9.4?

给你一囗甜甜゛ 提交于 2019-12-29 05:25:18
问题 For versions greater than 9.5 see this question I have created a table in PostgreSQL using this: CREATE TEMP TABLE jsontesting AS SELECT id, jsondata::jsonb FROM ( VALUES (1, '["abra","value","mango", "apple", "sample"]'), (2, '["japan","china","india", "russia", "australia"]'), (3, '["must", "match"]'), (4, '["abra","value","true", "apple", "sample"]'), (5, '["abra","false","mango", "apple", "sample"]'), (6, '["string","value","mango", "apple", "sample"]'), (7, '["must", "watch"]') ) AS t(id

jsonb vs jsonb[] for multiple addresses for a customer

…衆ロ難τιáo~ 提交于 2019-12-28 04:34:25
问题 It's a good idea to save multiple addresses in a jsonb field in PostgreSQL. I'm new in nosql and I'd like to test PostgreSQL to do that. I don't want to have another table with addresses, I prefer to have it in the same table. But I'm in doubt, I've seen PostreSQL have jsonb and jsonb[]. Which one is better to store multiple addresses? If I use jsonb, I think I must to add a prefix for every field like this: "1_adresse_line-1" "1_adresse_line-2" "1_postalcode" "2_adresse_line-1" "2_adresse

pgsql jsonb常用sql

别说谁变了你拦得住时间么 提交于 2019-12-25 16:57:31
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> json,jsonb区别 json和jsonb,而两者唯一的区别在于效率,json是对输入的完整拷贝,使用时再去解析,所以它会保留输入的空格,重复键以及顺序等。而jsonb是解析输入后保存的二进制,它在解析时会删除不必要的空格和重复的键,顺序和输入可能也不相同。使用时不用再次解析。两者对重复键的处理都是保留最后一个键值对。效率的差别:json类型存储快,查询慢,jsonb类型存储稍慢,查询较快(支持许多额外的操作符)。 json和jsonb共同操作符 操作符 返回类型 数组[1,2,3] {"a":1,"b":2,"c":3} {"a":{"b":{"c":1}},"d":[4,5,6]} -> json select '[1,2,3]'::jsonb ->2 = 3 select '{"a":1,"b":2,"c":3}'::jsonb-> 'a'=1 select '{"a":{"b":{"c":1}},"d":[4,5,6]}'::jsonb ->'a'={"b": {"c": 1}} ->> text select '[1,2,3]'::jsonb ->>2 = 3 select '{"a":1,"b":2,"c":3}'::jsonb->> 'a'=1 select '{"a":{"b":{"c":1

Delete objects inside NESTED JSONB arrays with PostgreSQL

这一生的挚爱 提交于 2019-12-24 22:43:02
问题 json Request: INSERT INTO test.demotbl (data) VALUES ('{ "x1": "Americas", "x2": "West", "x3": [{ "x_id": "sam" }], "x4": { "a1": true, "a2": false, "a3": [ "xx", "xx" ], "a4": [ "Josh" ], "y1": [{ "id": "RW", "z2": true, "z3": "USER", "z4": [{ "name": "john" }] }, { "id": "RO", "z2": false, "z3": "SELECT", "z4": [{ "name": "salin" } { "id": "DBA", "z2": false, "z3": "SELECT", "z4": [{ "name": "Samule" }] } ] } }'::jsonb) Question 1 :how can i delete the the array values where "id":"RO" from

Deep JSON query with partial path in PGSQL JSONB?

一曲冷凌霜 提交于 2019-12-24 15:53:15
问题 Given a number of JSON document like this: { id: some_id, l1: { f1: [ { c1: foo, c2: bar }, { c1: foo1, c2: bar1 }, ], f2: [ { c3: baz, c4: bar }, ], } } How can I query PostgreSQL JSONB for f1....c1: foo1 -- ie lX is not given nor is the list position of the c1-c2 subdocument. This is not a duplicate of Deep JSON query with partial path in MySQL 5.7? since that is about MySQL and this one is about PgSQL JSONB. 回答1: Here you need to iterate over the list of elements for path {l1,f1} #> -