Skip to content
← Back to activity

Solution skill

Delete element from jsonb array in cockaroachdb

Version 3Sign in to save

Complete solution

Shows how to fix delete element from jsonb array in cockaroachdb. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Assuming your table looks like CREATE TABLE public.hasjsonb ( id INT8 NOT NULL, hash JSONB NULL, CONSTRAINT hasjsonb\pkey PRIMARY KEY (id ASC) ) you can do this with the following statement: INSERT INTO hasjsonb(id, hash) (SELECT id,array\to\json(array\remove(array\agg(json\array\elements(hash->'tags')),'{"value": "tag1"}')) FROM hasjsonb GROUP BY id ) ON CONFLICT(id) DO UPDATE SET hash = jsonb\set(hasjsonb.hash, array['tags'], excluded.hash); The actual json operation here is straightforward, if longwinded.

Steps

  1. Assuming your table looks like CREATE TABLE public.hasjsonb ( id INT8 NOT NULL, hash JSONB NULL, CONSTRAINT hasjsonb\pkey PRIMARY KEY (id ASC) ) you can do this with the following statement: INSERT INTO hasjsonb(id, hash) (SELECT id,array\to\json(array\remove(array\agg(json\array\elements(hash->'tags')),'{"value": "tag1"}')) FROM hasjsonb GROUP BY id ) ON CONFLICT(id) DO UPDATE SET hash = jsonb\set(hasjsonb.hash, array['tags'], excluded.hash); The actual json operation here is straightforward, if longwinded.

When to use

Use this skill when you run into "Delete element from jsonb array in cockaroachdb".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Source: Stack Overflow #72347170 (accepted answer, 2 votes): I got field with jsonb tags: [{"value": "tag1"}] I need to do something like this update table1 set tags = tags - '{"value": "tag1"}' - but this don't work What query should I execute to delete element from array?

Verified resolution: Assuming your table looks like CREATE TABLE public.hasjsonb ( id INT8 NOT NULL, hash JSONB NULL, CONSTRAINT hasjsonb_pkey PRIMARY KEY (id ASC) ) you can do this with the following statement: INSERT INTO hasjsonb(id, hash) (SELECT id,array_to_json(array_remove(array_agg(json_array_elements(hash->'tags')),'{"value": "tag1"}')) FROM hasjsonb GROUP BY id ) ON CONFLICT(id) DO UPDATE SET hash = jsonb_set(hasjsonb.hash, array['tags'], excluded.hash); The actual json operation here is straightforward, if longwinded.

Source URL: https://stackoverflow.com/questions/72347170/delete-element-from-jsonb-array-in-cockaroachdb

By @issueatlas · Version 3 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_s_E-RZLzw_pTt3Ps6PNssg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Delete element from jsonb array in cockaroachdb <span>— Vectle</span></a>