N1QL query to insert from resultset and add uuid as id to the document

@vsr1

data = [ { “historyid”: “47b”, “id”: “6b9” }, { “historyid”: “47b”, “id”: “c40” }…]

I am trying to insert new document by selecting data with id= historyid and and insert a new document from resultset with new id=uuid()

data = select * from hist where id = historyid

INSERT INTO hist
SELECT * from hist AS doc where id = historyid;
how can I add uuid as id in the insert

INSERT INTO hist (KEY UUID(), VALUE doc)
SELECT d AS doc 
FROM hist AS d
WHERE ...............;

https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/insert.html#insert-examples

thanks @vsr1
forgot to add — apart from adding uuid I need to update ref field with id from data

INSERT INTO hist (KEY UUID(), VALUE OBJECT_PUT(doc, "refrencefield",  "xyz"))
SELECT d AS doc 
FROM hist AS d
WHERE ...............;

thanks again @vsr1
marked as solution

@vsr1 I tried this solution inside of for loop like below and getting timeout erors alot.
I am trying to bulk insert documents based on result query in a executing eventing function.
Do you see any better way of doing this.

data = query result;

for (var val of data) {
if (!val.historyid && !val.id) {
log(‘no records to insert into history’);
break;
}
var historyID = val.historyid;
var vID = val.id;

  // insert into history with instanceRef from data  
  INSERT INTO hist (KEY UUID(), VALUE OBJECT_PUT(docx, "refrencefield", $vID))
  SELECT d AS docx FROM hist AS d WHERE d.id = $historyID;
  log('insert successful', vID);
}

errors - Many of these are in log

  1. 2021-05-13T22:41:26.942+00:00 [INFO] {“message”:" Query error : {\n"requestID": “bfdec9d0-98dd-48fb-a452-c3f73f5d828f”,\n"signature": null,\n"results": [\n],\n"errors": [{“code”:1080,“msg”:“Timeout 58s exceeded”,“retry”:true}],\n"status": “timeout”,\n"metrics": {“elapsedTime”: “58.039967332s”,“executionTime”: “58.039855448s”,“resultCount”: 0,“resultSize”: 0,“errorCount”: 1}\n}\n",“stack”:“Error\n at OnUpdate (history-cms-update.js:19:13)”}

  2. 2021-05-13T22:43:24.200+00:00 [INFO] {“message”:“SDK error : Client-Side timeout exceeded for operation. Inspect network conditions or increase the timeout Query error : Error accounting LCB : error message is empty”,“stack”:“Error\n at OnUpdate (history-cms-update.js:9:20)”}

Increase timeout in the client. @jon.strabala may able to help in eventing