Replace text of an element in an array

I have documents with structure like:

{
"customerId":"123456",
"events": [
{
"id":"9876",
"eventName":"Sample name of the event",
"eventDescription": "Sample name of the event with description"
}
]
}

Now I want to find all the events that match a list of IDs (which contains 1000s of values) and update the eventName and eventDescription fields of those by replacing Sample name with Sample name+

I came up with something like this, but I’m getting syntax error :frowning:

UPDATE default 
SET REPLACE(e.eventName, "Sample name" , "Sample name+")
FROM default UNNEST events e
WHERE e.id IN ["9876"]

Please help me with this, thank you!

UPDATE default  AS d
SET e.eventName = REPLACE(e.eventName, "Sample name" , "Sample name+")
             FOR e IN d.events WHEN e.id IN "9876"] END
WHERE ANY v IN d.events SATISFIES  v.id IN ["9876"] END

https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/update.html

Thank you, it’s complaning like this:

{
  "errors": [
    {
      "code": 3000,
      "msg": "SET clause has invalid path REPLACE - at )"
}]}

:frowning:

UPDATE default  AS d
SET e.eventName = REPLACE(e.eventName, "Sample name" , "Sample name+")
             FOR e IN d.events WHEN e.id IN "9876"] END
WHERE ANY v IN d.events SATISFIES  v.id IN ["9876"] END
1 Like

Thanks a lot, this worked. Do you know if there’s a limit on the number of matching values we give in WHEN or WHERE?

There is no Limit in matching values.

Thanks a ton for the pointer.
As a side note, is it safe to update large number of documents during rebalancing?

You can update. Update with where clause ( if the new value is not present). Update with small batches with LIMIT (retry on errors) if needed use scan_plus.

Sorry what do you mean ‘if the new value is not present’?

UPDATE default d
SET d.a = "xyz"
WHERE d.b = 5;

If above statement run multiple times mutation is repeated.

Below statement second time will not mutate. This may not always possible ( when more than one set/unset)

UPDATE default d
SET d.a = "xyz"
WHERE d.b = 5 AND d.a != "xyz";

If you have large number of documents easiest option will be eventing Examples: Using the Eventing Service | Couchbase Docs

Check post 24 from @jon.strabala N1QL CBQ Update million records is not working - #24 by jon.strabala