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):
- Create a “tmp” bucket and migrate selected records to some “tmp” bucket.
- Use “cbbackup” command to take backup of the data from “tmp” bucket.
- Download and open “DB Browser for SQLite“
- Click on “Open Database” button, go to the backup folder and select *.cbb file from the folder.
- 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.
- After importing *.cbb file in SQLite, you will see that there are two tables: cbb_meta and cbb_msg
- We are interested in cbb_msg table
- Execute a query to update “key” value, you can also use existing functions. For example, I want to replace only portion of the key:
- Select records and see if your changes correct.
- Now click on “Write Changes” button so that changes are saved in *.cbb file
- Repeat this process to update keys from all the *.cbb files
- Now, flush the “tmp” bucket and import updated *.cbb files
- Use “cbrestore” command to import the *.cbb file in “tmp” bucket.
- 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.