Divya
Divya

Reputation: 79

how to update array in couchbase using n1ql query

{
"name":"nick",
"emailId":"[email protected]",
"subjects" : [{
   "name":"SOA",
   "tutor":"roshan",
   "classes" : "12"
 },
 {
   "name":"UNIX",
   "tutor":"mathew",
   "classes" : "9"
 }
],
"id" : "12345"
}

I want to add another set of subjects using update query. i tired to put some query by seeing some website but it show error and i am unable to understand. It will be more helpful if you give suggestion and thanks for the help. the query which i tired was :- [update studapp ARRAY a.name= "networks", a.tutor= "shalin" ,a.classes= "8" FOR a IN subjects END where id = '12345';]

Upvotes: 1

Views: 3649

Answers (2)

Divya
Divya

Reputation: 79

update `studapp`
SET subjects= ARRAY_APPEND( subjects, {   "name":"SE",
   "tutor":"Mani",
   "classes" : "7" } ) 
where id = '12345';

I got result as i needed (adding set of new values to the existing array)

RESULT

{
"name":"nick",
"emailId":"[email protected]",
"subjects" : [{
   "name":"SOA",
   "tutor":"roshan",
   "classes" : "12"
 },
 {
   "name":"UNIX",
   "tutor":"mathew",
   "classes" : "9"
 }
 {   
   "name":"SE",
   "tutor":"Mani",
   "classes" : "7"
 }
],
"id" : "12345"
}

Upvotes: 5

Related Questions