n1ql query to update multiple parameters in array of json data

后端 未结 1 885
天涯浪人
天涯浪人 2021-01-03 13:14

Following is the sample document ( userdetails) in couchbase.

{
\"friends\": [
  {
    \"company\": \"microsoft\",
    \"firstname\": \"criss\",
    \"lastna         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 13:20

    Each SET term is independent, so you can do the following:

    UPDATE default
    USE KEYS "userdetails"
    SET a.firstname="abc" FOR a IN friends WHEN a.company="microsoft" END,
        a.lastname="xyz" FOR a IN friends WHEN a.company="microsoft" END
    RETURNING friends;
    

    To answer your comment, the following two forms avoid the double loop. You can measure with actual data to see which form gives the best performance.

    UPDATE default
    USE KEYS "userdetails"
    SET friends[i] = {"firstname":"abc", "lastname":"xyz", "company":"microsoft"} FOR i : a IN friends WHEN a.company="microsoft" END
    RETURNING friends;
    
    UPDATE default
    USE KEYS "userdetails"
    SET friends[i] = OBJECT_PUT(OBJECT_PUT(a, "firstname", "abc"), "lastname", "xyz") FOR i : a IN friends WHEN a.company="microsoft" END
    RETURNING friends;
    

    0 讨论(0)
提交回复
热议问题