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?

Copy from bucket to bucket

Yes! In fact, if you’ve done this sort of thing in SQL, then you are not too far from already knowing the answer.

Fire buckets Image Licensed through Creative Commons via Paul Harrop - http://www.geograph.org.uk/photo/2666296
Fire buckets Image Licensed through Creative Commons via Paul Harrop

There are command line tools to backup/restore a bucket, but for this post, I’m going to assume that what you want is to make a copy of some documents from bucket A into bucket B.

It’s as easy as using a N1QL INSERT.

Start by creating a N1QL SELECT to get the documents you want out of the first bucket. I’m going to show a very simplified SELECT that gets all the documents from my default bucket. If you only want a subset, you can use a WHERE clause.

Since we’re copying to another bucket, we need both the key and the document itself. This query selects the document key using the META() function, and selects the document using a _v alias.

Next, I’ll create an INSERT to put these selected documents into another bucket, which I called target.

An INSERT needs a key and a value. I have those from the SELECT. All done.

More information about N1QL INSERT

The link:N1QL INSERT is very powerful, and there are a lot of other things you can do with it:

  • Insert a single document
  • Bulk inserts
  • Insert values using SELECT (which is similar to what we did in this post)
  • Insert value with a combination key, using a projection
  • Insert values using subqueries
  • Insert values using N1QL functions
  • Using prepared INSERT queries

Note that typical caveats apply: make sure you have good indexing when you are writing the SELECT. If you are copying a lot of documents, you may want to temporarily adjust the timeout. Documents in a bucket must all have a unique key. There is no rollback when inserting multiple documents: if an INSERT fails on the 10th document out of 100, the first 9 documents are still inserted.

For more in-depth answers about N1QL, check out the Couchbase N1QL Forum.

Follow me on Twitter if you have any questions: your question might become my next blog post!


Posted by Matthew Groves

Matthew D. Groves is a guy who loves to code. It doesn't matter if it's C#, jQuery, or PHP: he'll submit pull requests for anything. He has been coding professionally ever since he wrote a QuickBASIC point-of-sale app for his parent's pizza shop back in the 90s. He currently works as a Senior Product Marketing Manager for Couchbase. His free time is spent with his family, watching the Reds, and getting involved in the developer community. He is the author of AOP in .NET, Pro Microservices in .NET, a Pluralsight author, and a Microsoft MVP.

Leave a reply