2
votes

Following is the sample document (userdetails) in couchbase.

{  "friends":[  
  {  
     "company":"microsoft",
     "firstname":"criss",
     "lastname":"angel"
  },
  {  
     "company":"google",
     "firstname":"captain",
     "lastname":null
  }  ] }

based on the company name, i want to remove the respective json document from the array.

n1ql query

update default use keys "userdetails" set friends=array_remove(friends,a) for a in friends when a.company="google" end returning friends

Im not able to remove the json data using the above query.

This query works properly, if we have empty string ( "lastname" : " ") rather than null value.

So, how to remove, if any of the parameter value is "null"

1

1 Answers

3
votes

You can replace the whole friends array as follows:

UPDATE default
USE KEYS "userdetails"
SET friends = ARRAY a FOR a IN friends WHEN a.company <> "google" END
RETURNING friends;