I need to update an array in jsonb postgresql in nested object. I can't seem to have this working. I have tried many different things so far without success. I am trying to update the cleaner array by comparing with the schedule id.
table
id(serial) | info(jsonb)
server.js
var contractorInfo = {
"id": cleanerid,
"fname": fname,
"lname": lname,
"avatar":avatar
}
//id
var laveid = 'order_cbs1l';
Returns null doesn't update
"UPDATE users SET info = JSONB_SET(info, '{schedule,cleaner}', '"+JSON.stringify(contractorInfo)+"') WHERE info->'schedule'->>'id'='"+laveid+"' RETURNING*"
Returns null doesn't update
"UPDATE users SET info = JSONB_SET(info, '{schedule,cleaner}', '"+JSON.stringify(contractorInfo)+"') WHERE info #>> '{schedule,id}' = '"+laveid+"' RETURNING*"
json object
{
"dob": "1988-12-11",
"type": "seller",
"email": "[email protected]",
"phone": "5553766962",
"avatar": "image.png",
"schedule": [
{
"id": "order_cbs1l",
"pay": "230",
"date": "2022-12-29",
"status": "Available",
"address": "234 Eleventh Street, Mildura Victoria 3500, Australia",
"cleaner": {
"id": "",
"fname": "",
"lname": "",
"avatar": ""
},
"end_time": "10:15",
"start_time": "01:00",
"total_hours": "33300000",
"paymentIntentId": "pi_3KJnrEFzZWeJoxzV1yUdGLQ8"
}
],
"last_name": "doe",
"first_name": "john",
"countrycode": "Canada: +1",
"countryflag": "iti__ca",
"date_created": "2022-11-12T19:44:36.714Z"
}
SET info = JSONB_SET(info, '{schedule,0,cleaner}', $2) WHERE info->'schedule'->0->>'id'=$1if you want to test and update the first object in the array. What do you want to do if the array is empty or has multiple members?