Couchbase – Clone Records and Update Document Key

Couchbase is one of the best NoSQL database. I have used this for one of the project and recently came across a scenario for which I had to do some research but straightforward solution was not available. So, I just thought of documenting various steps so that other can be benefited.

So, in my scenario, I had to select some X records from one couchbase bucket and migrate them to another bucket. This was really easy. I wrote a N1QL query, added some WHERE clauses, and thats, done! All the selected records were migrated from one bucket to another.

Here comes the complexity. Someone updated the requirements, and now I was asked to update the “key” of the document. I was searching, searching and searching for correct and quick solution but found that we cannot update document “key” using N1QL. So, now I had to find some work around, and this is what I came up with (someone may have better solution for this scenario):

  1. Create a “tmp” bucket and migrate selected records to some “tmp” bucket.
  2. Use “cbbackup” command to take backup of the data from “tmp” bucket.
  3. Download and open “DB Browser for SQLite
  4. Click on “Open Database” button, go to the backup folder and select *.cbb file from the folder.
  5. Note that, if you have clustered environment, then you will notice multiple folders in backup location. You will have to repeat these steps for each *.cbb file.
  6. After importing *.cbb file in SQLite, you will see that there are two tables: cbb_meta and cbb_msg
  7. We are interested in cbb_msg table
  8. Execute a query to update “key” value, you can also use existing functions. For example, I want to replace only portion of the key:
  9. Select records and see if your changes correct.
  10. Now click on “Write Changes” button so that changes are saved in *.cbb file
  11. Repeat this process to update keys from all the *.cbb files
  12. Now, flush the “tmp” bucket and import updated *.cbb files
  13. Use “cbrestore” command to import the *.cbb file in “tmp” bucket.
  14. Finally, migrate records from “tmp” bucket to desired bucket.

Yes, I know this is long process, but worked for me. Hope this will be helpful to at least some of you.

Leave a Reply

Your email address will not be published. Required fields are marked *