n1ql query to remove data from array which has parameter value null

纵然是瞬间 提交于 2019-12-10 11:04:02

问题


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:


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;


来源:https://stackoverflow.com/questions/37181435/n1ql-query-to-remove-data-from-array-which-has-parameter-value-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!