Copying Documents Between Buckets in Couchbase
Want to copy your data from one Couchbase bucket to another? It just takes a bit of N1QL.
Join the DZone community and get the full member experience.Join For Free
when i’m at a user group or conference, people often come up to me afterwards with good questions. this is a great way for me to get blog post ideas: if they have a question, chances are lots of other people have the same one. it’s also a great way for me to get to know couchbase better.
this post is for one of those questions: can i copy documents from one bucket to another ?
yes! in fact, if you’ve done this sort of thing in sql, then you are not too far from already knowing the answer.
it’s as easy as using a n1ql insert .
start by creating a n1ql
to get the documents you want out of the first bucket. i’m going to show a very simplified
the documents from my
bucket. if you only want a subset, you can use a
select meta().id _k, _v from `default` _v
since we’re copying to another bucket, we need both the key and the document itself. this query selects the document key using the
function and selects the document using a
next, i’ll create an
to put these selected documents into another bucket, which i called
insert into `target` (key _k, value _v) select meta().id _k, _v from `default` _v
needs a key and a value. i have those from the
. all done.
Published at DZone with permission of Matthew Groves, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.