Typeorm Jsonb column array counts - postgresql

In my database i have a jsonb array column and i want to count element inside that column during my select query from typeorm.
I am using postgres.

Related

PostgreSQL get array column value from second index

I have a table with a column that contains array of numbers like : ['1','3','45'];
And was looking for a way to select the column value from the second index so that I get ['3','45']
Is there any way to do this in PostgreSQL?

PostgreSQL: Index JSONB array that is queried with `#?` operator

My table (table) has a JSONB field (data) that contains a field with an array where I store tags (tags).
I query that table with an expression like:
SELECT * FROM table WHERE data->'tags' #? '$[*] ? (# like_regex ".*(foo|bar).*" flag "i");
With such use-case is there a way for me to index the data->'tags' array to speed up the query? Or should I rather work on moving the tags array out of the JSONB field and into a TEXT[] field and index that?
I've already tried:
CREATE INDEX foo ON tbl USING GIN ((data->'tags') jsonb_path_ops);
but it doesn't work: https://gist.github.com/vkaracic/a62ac917d34eb6e975c4daeefbd316e8
The index you built can be used (if you set enable_seqscan=off, you will see that it does get used), but it is generally not chosen as it is pretty useless for this query. The only rows it would rule out through the index are the ones that don't have the 'tags' key at all, and even at that is poorly estimated so probably won't be used without drastic measures.
You could try to convert to text[] and the use parray_gin, but probably better would be to convert to a child table with text and then use pg_trgm.

Convert JSON string to JSONB

I have a table with jsonb field. Some of the rows are an array of objects, but some of the others are string.
I want to convert red rows to array of objects.
My table structure:
How I can do this in PostgreSQL?
Following SQL should do the trick:
update your_table_name
set content = (content#>>'{}')::jsonb
where jsonb_typeof(content)='string';
Reference:
https://www.postgresql.org/docs/10/functions-json.html

How to save results of query into a new column, postgres

How to add results of selecting query to a new column in already existing table in PostgreSQL in pgAdmin?
Results of the select query is an alteration of over columns in the same table.
Just create the column and update data afterwards (using the command line bundled in pgadmin):
ALTER TABLE tablename ADD COLUMN colname coltype;
UPDATE tablename SET colname = yourexpression;

Create an index over distinct values of a column in PostgreSQL

I have a PostgreSQL table that looks like this:
CREATE TABLE items (
name TEXT NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY (name, value)
);
I frequently do a query to see what values are available:
SELECT DISTINCT value FROM items;
How do I create an index in PostgreSQL that the above query to not have to iterate over all of the items table?
Using a completely different query you can force PostgreSQL to use an index and get the equivalent of DISTINCT column..
https://wiki.postgresql.org/wiki/Loose_indexscan