Couchbase and the various server SDKs offer many different ways to query for data. You could write N1QL queries, view queries, or you could even lookup documents by their key. Of the three possible ways to obtain data, doing lookups based on defined keys will always be faster than executing a query that uses an index.

What happens when you have multiple document keys and you need to obtain all the corresponding documents? The first thing to come to mind might be to loop through these keys, performing a lookup operation in each iteration of the loop. That works, but at the price of a new network request per lookup. Is there a better way? Absolutely!

What you can do is a bulk operation against the database. I’ve seen this question come up numerous times and I even wrote a tutorial on how to accomplish such a task in Node.js. However, what if you’re not a JavaScript developer? This time we’re going to see how to perform a batch request using the Go programming language.

Take the following Golang application for example:

In the above application we have a Person structure which represents each of our Couchbase documents. After establishing a connection to the Couchbase cluster and opening a particular bucket we create a slice of bulk operations called items.

Each key that we wish to lookup in our request will go into this items slice and the resulting value will be stored in it as well.

Using the Do function we can execute the bulk operation, keeping an eye out for errors. In this case errors are not missing keys, they are execution errors. After executing the request we can loop through each item in the items slice. If the value of a particular item equals that of an initialized, but empty Person object, it means that the key was not found on the server. Otherwise we’re just going to print out the key and the found document.

If you’re interested in the topic of performance, Kirk Kirkconnel wrote a blog post that explains the differences. It can be found here. You can also read about batching operations in the Couchbase documentation.

Author

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Leave a reply