N1QL Query for copying data from one bucket to another

we use following query for copying data from one bucket to another

INSERT INTO A (KEY META().id) SELECT * FROM B)

Where A and B are bucket names
But this is generating used keys

Is there any query to copy data by generating new keys?

You can use UUID() or generate custom key from any field from the document or key

INSERT INTO a (KEY UUID(), VALUE t) SELECT t FROM B AS t;

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

Also use Eventing Bulk copy of existing documents

1 Like

@Kratos - a trivial example of using Eventing to copy all documents from one bucket to another can be found in this post Mirror Couchbase bucket with bulk documents - #2 by jon.strabala

Note Eventing uses DCP (database change protocol) and can accomplish this replication extremely fast