Insert is not working as excepted

I am trying to insert lGroups but it is not working.

{
“name”: “testapp”,
“pGroups”: [
{
“contex” : null,
“lGroups”: [],
“name”: “default”
}
]
}
UPDATE SAM app
SET lGroup = ARRAY_PUT(lGroup,{“name”:“testname”,“description”:“test description”})
FOR lGroup in pGroup.lGroups
FOR pGroup in pGroups
WHEN pGroup.name = 'default’
END
WHERE name = ‘testapp’

try this update

UPDATE SAM app
   SET pg.lGroups = ARRAY_PUT(pg.lGroups,{"name":"testname","description":"test description"})
       FOR pg IN app.pGroups
         WHEN pg.name = 'default'
       END
 WHERE name = 'testapp'
RETURNING *;

Thank You very much…
Let me try ,…

It is working perfectly…Thanks
Can you help me please to understand that above query which I posted was working sometimes and not working some other times…? Why it is behaving like this…

{
“name”: “testapp”,
“pGroups”: [
{
“contex”: null,
“lGroups”: [
{
“description”: “test description”,
“name”: “testname”,
“user”:[]
}
],
“name”: “default”
}
]
}
If for this document I need to add user then I will add another for loop or how it will be…Because I was following the same way I posted for my previous query.

FYI
https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/update.html

try this

UPDATE SAM app
    SET pg.lGroups = (ARRAY OBJECT_ADD(ig, 'user', [] ) 
                        FOR ig IN pg.lGroups 
                           WHEN ig.name == "testname" 
                        END)
         FOR pg IN app.pGroups 
           WHEN pg.name == "default" 
         END
WHERE app.name == "testapp"
RETURNING *;
1 Like