jsonb

jsonb LIKE query on nested objects in an array

孤街浪徒 提交于 2019-12-07 20:16:03
问题 My JSON data looks like this: [{ "id": 1, "payload": { "location": "NY", "details": [{ "name": "cafe", "cuisine": "mexican" }, { "name": "foody", "cuisine": "italian" } ] } }, { "id": 2, "payload": { "location": "NY", "details": [{ "name": "mbar", "cuisine": "mexican" }, { "name": "fdy", "cuisine": "italian" } ] } }] given a text "foo" I want to return all the tuples that have this substring. But I cannot figure out how to write the query for the same. I followed this related answer but

Update multiple values in a jsonb data in PostgreSQL

北城以北 提交于 2019-12-07 13:11:36
问题 I need to update a jsonb data(column->users) in my table 'settings' My jsonb data is like '{ "Email": "aaaa", "UserId": "49", "Created": "11/13/2016", "EntityId": "1", "IsActive": "False", "Modified": "11/13/2016", "Username": "aa" }' In this json string I need to update Email,IsActive,Username together. I tried the below update query,its working fine. But that is for a single value updation. UPDATE settings SET users = jsonb_set(users, '{Email}', '"aa"') WHERE users @> '{"UserId":"49"}'; How

Indexing jsonb for numeric comparison of fields

拟墨画扇 提交于 2019-12-07 01:52:35
I've defined a simple table with create table resources (id serial primary key, fields jsonb); And it contains data with keys (drawn from a large set) and values between 1 and 100, like: id | fields --------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1 | {"tex": 23, "blair": 46, "cubic": 50, "raider": 57, "retard": 53, "hoariest": 78, "suturing": 25, "apostolic": 22, "unloosing": 37, "flagellated": 85} 2 | {"egoist": 75, "poshest": 0,

PostgreSQL JSONB - query condition with variable key names

孤者浪人 提交于 2019-12-07 01:35:25
问题 I have gone through various JSONB tutorials: https://blog.codeship.com/unleash-the-power-of-storing-json-in-postgres/ https://www.wagonhq.com/sql-tutorial/values-from-nested-json http://schinckel.net/2014/05/25/querying-json-in-postgres/ http://stormatics.com/howto-use-json-functionality-in-postgresql/ Consider the following example. There is a table called plans . It has the following columns: id (integer, auto-incrementing primary key). name (string). structure (jsonb). The structure column

Roughly how fast is JSON -> JSONB column conversion in Postgres 9.4

混江龙づ霸主 提交于 2019-12-07 00:24:46
问题 I'm looking to migrate from Postgres 9.3 to 9.4, and have a lot of data in JSON columns. While it's fine, I wanted to have a look at migrating to the more efficient column storage (which JSONB seems to be — a really exciting piece of tech!). To actually migrate, I want to know migration characteristics for something like ALTER TABLE table_with_json ALTER COLUMN my_json SET DATA TYPE jsonb USING my_json::jsonb; (from this helpful question). Ideally, it would be good to know how long it takes

Join tables using a value inside a JSONB column

心不动则不痛 提交于 2019-12-06 12:04:05
问题 There are two tables: Authorized Contacts ( auth_contacts ): ( userid varchar contacts jsonb ) contacts contains an array of contacts with attributes {contact_id, type} discussion : ( contact_id varchar discussion_id varchar discussion_details jsonb ) The table auth_contacts has at least 100k records making it non JSONB type is not appropriate according as it would double or triple the amount of records. Sample data for auth_contacts : userid | contacts '11111' | '{"contact": [{"type": "type

jsonb LIKE query on nested objects in an array

感情迁移 提交于 2019-12-06 09:36:04
My JSON data looks like this: [{ "id": 1, "payload": { "location": "NY", "details": [{ "name": "cafe", "cuisine": "mexican" }, { "name": "foody", "cuisine": "italian" } ] } }, { "id": 2, "payload": { "location": "NY", "details": [{ "name": "mbar", "cuisine": "mexican" }, { "name": "fdy", "cuisine": "italian" } ] } }] given a text "foo" I want to return all the tuples that have this substring. But I cannot figure out how to write the query for the same. I followed this related answer but cannot figure out how to do LIKE . This is what I have working right now: SELECT r.res->>'name' AS feature

recursively flatten a nested jsonb in postgres without unknown depth and unknown key fields

你。 提交于 2019-12-06 08:59:56
问题 How can I recursively flatten a nested jsonb in postgres which I don't know the depth and the field at each depth? (see example below) A postgressql query to do the flattening would be much apreciated { "xx": "", "xx": "", "form": "xxx", "type": "", "content_type": "xxx", "reported_date": , "contact": { "imported_date": "", "name": "", "phone": "", "alternate_phone": "", "specialization": "", "type": "", "reported_date": , "parent": { "_id": "xxx", "_rev": "xxx", "parent": "", "type": "xxx" }

Flattening Postgres nested JSONB column

狂风中的少年 提交于 2019-12-06 07:34:15
I'm looking to see how to flatten data nested in a JSONB column. As an example, say we have the table users with user_id(int) and siblings(JSONB) With rows like: id | JSONB --------------------- 1 | {"brother": {"first_name":"Sam", "last_name":"Smith"}, "sister": {"first_name":"Sally", "last_name":"Smith"} 2 | {"sister": {"first_name":"Jill"}} I'm looking for a query that will return a response like: id | sibling | first_name | last_name ------------------------------------- 1 | "brother" | "Sam" | "Smith" 1 | "sister" | "Sally" | "Smith" 2 | "sister" | "Jill" | null I develop to this use it

How do you create a Postgresql JSONB array in array index?

社会主义新天地 提交于 2019-12-06 07:32:32
I have structure like this: user_id, a. a is of type jsonb and has the following structure: { b: [ {ids: [1,2,3,4]}, {ids: [2,3,4]}, {ids: [1,2,4]}, ... ] } How would I make an index that enabled me to find all users (user_id) that has a certain id in the ids list? Is a GIN index what you want? It seems that you first need to organized the IDs into a form that is more tractable. I'm more familiar with Python than I am with the PostgreSQL ways of manipulating JSON, so I used PL/Python for this purpose. DROP TABLE IF EXISTS ids; CREATE TABLE ids (user_id integer, a jsonb); INSERT INTO ids VALUES