jsonb

SQLAlchemy filter according to nested keys in JSONB

家住魔仙堡 提交于 2019-12-03 14:06:51
I have a JSONB field that sometimes has nested keys. Example: {"nested_field": {"another URL": "foo", "a simple text": "text"}, "first_metadata": "plain string", "another_metadata": "foobar"} If I do .filter(TestMetadata.metadata_item.has_key(nested_field)) I get this record. How can I search for existence of the nested key? ( "a simple text" ) van With SQLAlchemy the following should work for your test string: class TestMetadata(Base): id = Column(Integer, primary_key=True) name = Column(String) metadata_item = Column(JSONB) as per SQLAlchemy documentation of JSONB (search for Path index

how to store PostgreSQL jsonb using SpringBoot + JPA?

元气小坏坏 提交于 2019-12-03 14:03:01
问题 I'm working on a migration software that will consume unknown data from REST services. I already think about use MongoDB but I decide to not use it and use PostgreSQL. After read this I'm trying to implement it in my SpringBoot app using Spring JPA but I don't know to map jsonb in my entity. Tried this but understood nothing! Here is where I am: @Repository @Transactional public interface DnitRepository extends JpaRepository<Dnit, Long> { @Query(value = "insert into dnit(id,data) VALUES (:id,

Extract key, value from json objects in Postgres

给你一囗甜甜゛ 提交于 2019-12-03 12:26:17
I have a Postgres table that has content similar to this: id | data 1 | {"a":"4", "b":"5"} 2 | {"a":"6", "b":"7"} 3 | {"a":"8", "b":"9"} The first column is an integer and the second is a json column. I want to be able to expand out the keys and values from the json so the result looks like this: id | key | value 1 | a | 4 1 | b | 5 2 | a | 6 2 | b | 7 3 | a | 8 3 | b | 9 Can this be achieved in Postgres SQL? What I've tried Given that the original table can be simulated as such: select * from ( values (1, '{"a":"4", "b":"5"}'::json), (2, '{"a":"6", "b":"7"}'::json), (3, '{"a":"8", "b":"9"}':

jsonb existential operators with parameterised queries

余生颓废 提交于 2019-12-03 11:06:37
...or the question mark question. I am currently implementing the search functionality for a postgres database, in php, that uses the new jsonb type. To achieve this I am executing prepared statements with named placeholders. However I have run into an interesting problem whilst trying to use some of the new postgres JSON containment and existence operators along with named placeholders. The basis of issue being that the operators themselves use the question mark ? as part of their syntax. i.e. ? Does the key/element string exist within the JSON value? ?| Do any of these key/element strings

Does JSONB make PostgreSQL arrays useless?

扶醉桌前 提交于 2019-12-03 10:58:37
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 you use? And why? Erwin Brandstetter In most cases I would use a normalized schema with a table option

Postgresql json like query

╄→尐↘猪︶ㄣ 提交于 2019-12-03 10:32:48
问题 I have the following table called module_data. Currently it has three rows of entries: id data 0ab5203b-9157-4934-8aba-1512afb0abd0 {"title":"Board of Supervisors Meeting","id":"1i3Ytw1mw98"} 7ee33a18-63da-4432-8967-bde5a44347a0 {"title":"Board of Supervisors Meeting","id":"4-dNAg2mn6o"} 8d71ca35-74eb-4751-b635-114bf04843f1 {"title":"COPD 101", "id":"l9O0jCR-sxg"} Column data's datatype is jsonb . I'm trying to query it using like operator. Something like the following: SELECT * FROM module

Rails and jsonb type “jsonb” does not exist

一世执手 提交于 2019-12-03 10:24:26
psql --version psql (PostgreSQL) 9.4.1 rails -v Rails 4.2.0 I added a jsonb column through migration like that class AddPreferencesToUsers < ActiveRecord::Migration def change add_column :users, :preferences, :jsonb, null: false, default: '{}' add_index :users, :preferences, using: :gin end end I get this error : PG::UndefinedObject: ERROR: type "jsonb" does not exist LINE 1: SELECT 'jsonb'::regtype::oid any help ? After looking around I discovered that my postgresql version is not 9.4 by running the right command postgres=# SHOW SERVER_VERSION; server_version ---------------- 9.1 So I had

Looking for a right EAV structure based on jsonb

落爺英雄遲暮 提交于 2019-12-03 08:03:12
I wondering what will be the right approach to build EAV on jsonb. I have Attribute -> Values tables as like in standard EAV. CREATE TABLE attribute_values ( id INTEGER, attribute_id INTEGER, value VARCHAR(255) ); CREATE TABLE attributes ( id INTEGER, name VARCHAR(255) ); Values will saved in attributes filed of Entity CREATE TABLE entity ( id INTEGER, title TEXT, attributes JSONB ); Tables Attribute created to control duplicate attributes their types and better determine what it's a attribute is. For example to avoid: {weight: 100} and {Weight: 100} or {weigh: 100} . Values for work with

Appending (pushing) and removing from a JSON array in PostgreSQL 9.5+

。_饼干妹妹 提交于 2019-12-03 05:41:51
问题 For versions less 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

Postgresql JSONB is coming. What to use now? Hstore? JSON? EAV?

孤街醉人 提交于 2019-12-03 04:43:23
问题 After going through the relational DB/NoSQL research debate, I've come to the conclusion that I will be moving forward with PG as my data store. A big part of that decision was the announcement of JSONB coming to 9.4. My question is what should I do now, building an application from the ground up knowing that I want to migrate to (I mean use right now!) jsonb? The DaaS options for me are going to be running 9.3 for a while. From what I can tell, and correct me if I'm wrong, hstore would run