I have a nested JSON documen .and I want to append an array inside the Branch array for a particular brcId. how to do the array operation ? the following is my JSON Document
{
"_id": “acf45a6c-d7af-4bca-b6ba-fb0fc1e20e7f”,
“type”: “business”,
“biz”: [
{
“bizId”: “251d38bc-96aa-4add-aeaa-a18cb806f5c3”,
“Branch”: [
{
“brcId”: “b1b192fc-337b-4fb5-a450-d105e7cc4e46”,
“brName”: “branch1”,
},
{
“brcId”: “123435678890”,
“brName”: “branch2”,
}
]
}
]
}
can you give an example result data?
UPDATE default AS d SET
b.Branch = ARRAY_APPEND(b.Branch,{ "brcId": "zzz", "brName": "new zzz" })
FOR b IN d.biz WHEN b.`bizId` = "251d38bc-96aa-4add-aeaa-a18cb806f5c3" END
WHERE d.`_id` = "acf45a6c-d7af-4bca-b6ba-fb0fc1e20e7f"
AND ANY b IN d.biz SATISFIES b.`bizId` = "251d38bc-96aa-4add-aeaa-a18cb806f5c3"
AND "zzz" NOT IN b.Brnach[*].brcId END;
If this is not what you are looking provide expected result as @atom_yang mentioned.
I want to insert the following array into “Branch” array with particular Id ie., brcId=“some Id”…
“line”:[{
“lineId”:“1234”,
“lName”:“line1”
}]
The result I want is as follows,
{
"_id": “acf45a6c-d7af-4bca-b6ba-fb0fc1e20e7f”,
“type”: “business”,
“biz”: [{
“bizId”: “251d38bc-96aa-4add-aeaa-a18cb806f5c3”,
“Branch”: [
{
“brcId”: “b1b192fc-337b-4fb5-a450-d105e7cc4e46”,
“brName”: “branch1”,
“line”:[{
“lineId”:“1234”,
“lName”:“line1”
}]
},
{
“brcId”: “123435678890”,
“brName”: “branch2”,
} ]
}
]
}
INSERT INTO default VALUES("001",{ "_id": "acf45a6c-d7af-4bca-b6ba-fb0fc1e20e7f", "type": "business", "biz": [{ "bizId": "251d38bc-96aa-4add-aeaa-a18cb806f5c3", "Branch": [ { "brcId": "b1b192fc-337b-4fb5-a450-d105e7cc4e46", "brName": "branch1" }, { "brcId": "123435678890", "brName": "branch2" } ] } ] });
UPDATE default AS d SET
brc.line = [{"lineId":"1234", "lName":"line1"}]
FOR brc IN b.Branch FOR b IN d.biz
WHEN b.`bizId` = "251d38bc-96aa-4add-aeaa-a18cb806f5c3"
AND brc.brcId = "b1b192fc-337b-4fb5-a450-d105e7cc4e46" END
WHERE d.`_id` = "acf45a6c-d7af-4bca-b6ba-fb0fc1e20e7f"
AND ANY b IN d.biz SATISFIES b.`bizId` = "251d38bc-96aa-4add-aeaa-a18cb806f5c3"
AND "b1b192fc-337b-4fb5-a450-d105e7cc4e46" IN b.Branch[*].brcId END;
Thanks it works…but I have one more query. If i want to push an array into the existing “line” array,how to do that because now if I run this query again with {“lineId”:“12345”, “lName”:“line2”} it replace the old array. I want the result as follows,
The result I want is as follows,
{
"_id": “acf45a6c-d7af-4bca-b6ba-fb0fc1e20e7f”,
“type”: “business”,
“biz”: [{
“bizId”: “251d38bc-96aa-4add-aeaa-a18cb806f5c3”,
“Branch”: [
{
“brcId”: “b1b192fc-337b-4fb5-a450-d105e7cc4e46”,
“brName”: “branch1”,
“line”:[{
“lineId”:“1234”,
“lName”:“line1”
},
{
“lineId”:“12345”,
“lName”:“line2”
}]
},
{
“brcId”: “123435678890”,
“brName”: “branch2”,
} ]
}
]
}
And how to check if the array contains the specified element “line” .