Couchbase N1q1 update array element and return only the updated block

Viewed 214

I want to update an element in an array object and return only the few elements from the updated array object.

Document:

{
"doctype" : "report",
"name":"nick",
"emailId":"nick123@gmail.com",
"subjects" : [{
   "name":"SOA",
   "tutor":"roshan",
   "classes" : "12",
    "mark" : 40,
    "subid": "5678"
 },
 {
   "name":"UNIX",
   "tutor":"mathew",
   "classes" : "9"
    "mark" : 50,
    "subid" : "3788"    
 }
],
"id" : "12345"
}

n1q1:

update bucket1 
   Set i.status = “pass” for i in subjects when i.mark > 40 end
where doctype = "report"
returning *  

above query returns the whole document but I want only few element from the updated array object like below. Can you please help me on this . Thanks

Expected query response

{
 "id" : "12345",
 "subid: “3788”,
 "name":"UNIX"
}
1 Answers

Returning clause you can build expression that you want. You are updating ARRAY, it means it can update more than one element. so you can return as ARRAY, if you want FIRST element replace with ARRAY with FIRST.

UPDATE bucket1 AS b
SET i.status = "pass" FOR i IN b.subjects WHEN i.mark > 40 END
WHERE b.doctype = "report" AND (ANY v IN b.subjects SATISFIES v.mark > 40 END)
RETURNING (ARRAY {v.id, v.subid, v.name} FOR v IN b.subjects WHEN v.mark > 40 END) AS subjects;

You can only return modified info, you will not able to return original info.

Related