jsonb

Postgres and jsonb - search value at any key

梦想与她 提交于 2019-12-13 13:15:07
问题 Is possible to look for a given value at any key in a JSONB column in Postgres? In the documentation I can't see any example. Example value at a JSONB column: { a: 1, b: 2, c: 3 } I want to find all records that have 1 as a value anywhere. NOTE: there may be other keys than a, b, c unknown at the moment. 回答1: use value of jsonb_each_text , sample based on previous sample of McNets,: t=# select * from json_test join jsonb_each_text(json_test.data) e on true where e.value = '1'; id | data | key

Issue with Sqlalchemy and inserting array of jsonb to postgresql

坚强是说给别人听的谎言 提交于 2019-12-13 06:11:50
问题 So i'm trying to insert an array of jsonb values into my database but I can't seem to format it right, here's my code: updated_old_passwords.append({"index": 1, "password": hashed_password}) user.old_passwords = updated_old_passwords user.last_password_reset = datetime.datetime.utcnow() db.session.commit() And here's the error: ProgrammingError: (psycopg2.ProgrammingError) column "old_passwords" is of type jsonb[] but expression is of type text[] LINE 1: ...-01-05T06:18:24.992968'::timestamp,

PostgreSQL - grouping by jsonb column

牧云@^-^@ 提交于 2019-12-13 02:38:47
问题 I have survey_results table which has following columns: id - integer score_labels - jsonb score_labels column data format looks like this: {"total": "High", "risk": "High"} Now I want to have sql query that will group and count my survey results by this score_labels column. This is what the final result should look like: total risk ------- ------ {high: 2, medium: 1, low: 0} {high: 1, medium: 2, low: 1} I want to count survey results by its score labels. Is there way to do it in PostgreSQL?

Spring Data JPA JSONB Paramaterization

懵懂的女人 提交于 2019-12-12 19:34:15
问题 What is the correct syntax (JPA, Spring Data, or SpEL) to convert this query into a Spring Data Repository nativeQuery ? SELECT * FROM mytable WHERE f_jsonb_arr_lower(myjsonb -> 'myArray', 'subItem', 'email') @> '"foo@foo.com"'; I want to use an input parameter instead of hard-coding "foo@foo.com" . My model: Postgres myTable with a JSONB column myJsonb : { "myArray": [ { "subItem": { "email": "bar@bar.com" } }, { "subItem": { "email": "foo@foo.com" } } ] } Index described here. The hard

Yii Postgress Json queries for operator with question marks ?, ?|, ?&

倖福魔咒の 提交于 2019-12-12 05:38:51
问题 How to write SQL queries to support JSON where conditions matching for operators ? , ?|, ?&. id : 12, name: 'Harry Pottor', type: ['Fiction', 'Horror', 'Adventure' ] In Yii, when I write query for JSON array check with ?& operator gives me error. This query run successfully in pgAdmin SELECT id, name FROM books WHERE type ?& array['Fiction', 'Horror'] $commandSql = $connection->createCommand($sql); But ? mark operator gets replaced with $1 i.e. going for bind value. How to do this type of

Using LIKE operator for array of objects inside jsonb field in PostgreSQL

冷暖自知 提交于 2019-12-12 04:18:04
问题 Is it possible to use LIKE operator for single key/value inside array of objects for jsonb field in PostgreSQL 9.4? For example I have: id | body ------------------------------------------------------------ 1 | {"products": [{"name": "qwe", "description": "asd"}, {"name": "zxc", "description": "vbn"}]} I know, I can get a product with something like this: select * from table where 'body'->'products' @> '[{"name": "qwe"}]'::jsonb The question is: can I get this product if I don't know full

PostgreSQL: How to SUM attributes including a JSONB field, and retain table shape?

北城以北 提交于 2019-12-12 04:05:55
问题 I am working with Postgres 9.4. I have a table with a JSONB field: Column │ Type │ Modifiers ─────────────────┼──────────────────────┼──────────────────────────────────────────────────────────────────── id │ integer │ not null default practice_id │ character varying(6) │ not null date │ date │ not null pct_id │ character varying(3) │ astro_pu_items │ double precision │ not null astro_pu_cost │ double precision │ not null star_pu │ jsonb │ I want to retrieve the summed values by date of

Postgres 9.4 jsonb queries basic operators

耗尽温柔 提交于 2019-12-12 03:35:59
问题 Is there any way to make queries on a jsonb field in some table in Postgres that are basically equitable to the Mongodb query operators (listed here https://docs.mongodb.org/manual/reference/operator/query-comparison/) I would like to be able to store some json objects in a postgres table for example: {"power": 200}, {"power": 400}, {"power": 0}, {"power": 146297}, If I do the current method of SELECT * FROM mytable where json_field ->> 'power' < '2'; I get retured both the row for power 0

How to search within a jsonb column in postgresql

99封情书 提交于 2019-12-12 02:35:52
问题 I basically have a postgresql table that has a column of type jsonb. the json data looks like this { "personal": { { "gender":"male", "contact":{ "home":{ "email":"ceo@home.me", "phone_number":"5551234" }, "work":{ "email":"ceo@work.id", "phone_number":"5551111" } }, "religion":"other", "languages":[ "English", "Xen" ], "last_name":"Eeo", "birth_date":"1945-07-28", "first_name":"Cee", "nationality":"Martian", "marital_status":"married" } } } I want to fetch all people who have the "Martian"

Postgres jsonb query on nested object

放肆的年华 提交于 2019-12-11 21:28:45
问题 My postgres db version: 9.4.4. and I have a table with this structure; CREATE TABLE product_cust ( productid character(2), data jsonb, ) I have records like this in the "data" column; {"productid":"01","cust": [ {"cell": ["0000xxx0", "0000xxx1"], "name": "John", "email": ["john@email.net"], "custtype": "c"}, {"cell": ["0000xxx2", "0000xxx3"], "name": "Smith", "email": ["smith@email.net"], "custtype": "c"} ]} I need to extract all records for "cell" . Expected record will be ["0000xxx0",