Add Attribute to existing Document

Hello,

I have a document as given below:

{
“d”: {
“depnum”: “98”,
“dept_description”: “Settlements”,
“hotelnum”: “380”,
“id”: “dept380”,
“subdepartments”:
[
{
“addons”:
[
{
“adddepnum”: 1,
“addpercent”: 13,
“addsubdepnum”: 15,
“addtype”: “A”
},
{
“adddepnum”: 2,
“addpercent”: 13,
“addsubdepnum”: 14,
“addtype”: “B”
}
],
“descchangeflag”: “N”,
“subdepnum”: 1,
},
{
“descchangeflag”: “N”,
“subdepnum”: 2,
}
],
“summarize”: “N”,
“type”: “department”
}
}

Now, I want to add an attribute. Precisely, I would like the document to be in the following manner:

{
“d”: {
“depnum”: “98”,
“dept_description”: “Settlements”,
“hotelnum”: “380”,
“id”: “dept380”,
“subdepartments”:
[
{
“addons”:
[
{
“adddepnum”: 1,
“addpercent”: 13,
“addsubdepnum”: 15,
“addtype”: “A”
},
{
“adddepnum”: 2,
“addpercent”: 13,
“addsubdepnum”: 14,
“addtype”: “B”
}
],
“descchangeflag”: “N”,
“subdepnum”: 1,
},
{
“descchangeflag”: “N”,
“subdepnum”: 2,
}
],
“new_field”: “dummy value”
“summarize”: “N”,
“type”: “department”
}
}

Please suggest a way to add the attribute using N1QL

UPDATE  d
SET d.new_field = "dummy value"
WHERE d.type = "department" AND .....;

https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/update.html

Thank you vsr1. But, it seems I had posted the result set a bit differently as compared to my requirement. The result JSON that I would require should look like below:

{
“d”: {
“depnum”: “98”,
“dept_description”: “Settlements”,
“hotelnum”: “380”,
“id”: “dept380”,
“subdepartments”:
[
{
“addons”:
[
{
“adddepnum”: 1,
“addpercent”: 13,
“addsubdepnum”: 15,
“addtype”: “A”
},
{
“adddepnum”: 2,
“addpercent”: 13,
“addsubdepnum”: 14,
“addtype”: “B”
}
],
“descchangeflag”: “N”,
“subdepnum”: 1,
},
{
“descchangeflag”: “N”,
“subdepnum”: 2,
“new_field”: “dummy value”
}
],
“summarize”: “N”,
“type”: “department”
}
}

I apologize for posting the wrong query.

Many thanks in advance

UPDATE  d
SET ao.new_field = "dummy value" FOR ao IN sd.addons FOR sd IN d.subdepartments WHEN ao.subdepnum = 2 END
WHERE d.type = "department" AND .....;

thanks a ton, vsr1 :):relieved: