How to modify field with jsonPath in PostgreSQL? - postgresql

How to modify a field using jsonPath in PostgreSQL like SQL Server JSON_MODIFY (https://learn.microsoft.com/en-us/sql/t-sql/functions/json-modify-transact-sql?view=sql-server-ver15)?
Thanks!

There are currently no ways to update JSON properties using JsonPath. The only way is with jsonb query jsonb_set

Related

PostgreSQL how to write a stored procedure to get the First and last name from a table

I want to write a stored procedure in PostgreSQL that get input parameters and then select data based on conditions using that input parameter values. How I can achieve this easily?
I only have to use the PostgreSQL stored procedure and and not function for this
In PostgreSQL if we want to return result set in tabular format we have to use functions. they are best way designed to do this all functionality

how to update JSONB column using knexjs, bookshelfjs

I have a JSONB column in PostgreSQL database like {lat: value, lon: value}. I want to change any specific value at a time eg. lat, but I am not sure how I can achieve this using bookshelf.js or knex.js. I tried using jsonb_set() method specified in Postgres documentation but I am not sure if I used that correctly. Can somebody please suggest me how can I do this? or what is the correct syntax to do this? Thanks.
AFAIK only knex based thing that supports writing to and extracting data from postgresql jsonb columns is objection.js ORM.
With plain knex you need to use raw to write references:
knex('table').update({
jsonbColumn: knex.raw(`jsonb_set(??, '{lat}', ?)`, ['jsonbColumn', newLatValue])
})
You can check generated SQL here https://runkit.com/embed/44ifdhzxejf1
Originally answered in: https://github.com/tgriesser/knex/issues/2264
More examples how to use jsonb_set with knex can be found in following answers
How to update a jsonb column's field in PostgreSQL?
What is the best way to use PostgreSQL JSON types with NodeJS
Jsonb field update using knex.js
return knex("tablename").update({
jsonbkey: knex.raw(`
jsonb_set(jsonbkey, '{city}','"Ayodhya"')
`)
}).where({"id" :2020})
The jsonbkey will be the column name, where the datatype is jsonb.
The tablename is the name of your table.
The city is the object key.
If there is multiple level of object then you can use dot. Like '{city.id}'
let result = await db().raw(`UPDATE widget
SET name = ?,
jsonCol= jsonCol::jsonb || ?::jsonb
WHERE id = ?`,
[name, JSON.stringify(newJsonData), id);
this knex query helps to update any json column by overriding specific keys in the value supplied to the right hand side of || operator. DO NOT forget to typecast the values with ::jsonb

How to map a column to JSONB instead of JSON, with Doctrine and Postgresql?

In my project I'm using Doctrine doctrine 2.5.4/postgres 9.5. I'm trying to add a jsonb field using YAML:
fields:
obj: json_array
This gets interpreted as a json column type (and not jsonb). The specification notes about picking up json or jsonb:
Chosen if the column definition contains the jsonb option inside the platformOptions attribute array and is set to true.
But platformOptions doesn't seem to work (tried to add it below obj, at the top... with no success). How can I add a jsonb field?
This is supported by doctrine/dbal v2.6+ (it requires PHP 7.1). All you need to do is use json_array and set options={"jsonb"=true} I tested this on doctrine/dbal v2.6.3
This is what it looks like in PHP format:
/**
* #ORM\Column(type="json_array",nullable=true,options={"jsonb"=true})
*/
private $details;
and it creates a query such as (for an existing table):
ALTER TABLE mytable ADD details JSONB NOT NULL;
More details about type mapping can be found at Doctrine mapping matrix.
Use boldtrn/jsonb-bundle, it provides a jsonb doctrine type as well as custom functions to access the special operators provided by the PostgreSQL jsonb data type.

How to delete records using jdbcTemplate. Query where clause with like clause

I want to delete huge data (above 100K) from table using spring jdbcTemplate. Where clause contains like. eg. DELETE from TABLE_NAME where NAME like 'ABC%'. If possible how to use batching. Please suggest. thanks

Do native queries use naming strategy to resolve the table name in the "from" clause of the given SQL query?

I have a custom naming strategy where I add a prefix to the table names. My question is: when I create a native query (using EntityManager.createNativeQuery) should I use the prefixed name of my tables in the FROM clause of my queries or should I use the non-prefixed name (as in JPQL queries) ??
A Native query is an SQL query, so you input what would execute in your datastore if you put it directly through JDBC. It is nothing to do with the Entities