There are many different ways to query for data in Couchbase Server. You can do direct lookups for documents
based on their key, you can query views, or you can even write N1QL queries.

When it comes to obtaining data, doing a lookup based on a document key will always be faster than querying.
What happens when you have a handful of keys that you need to lookup? You could easily loop through these
keys using one of the SDKs, but that is a new network request per operation. Did you know you could batch
these keys into a single request?

I’ve seen this asked a few times, so I thought it would be useful to write about how to make it happen. Let’s
use the scenario that we have 1000 keys that we need to look up in a Node.js application. To do this, our
code might look like the following:

In the above example we are connecting to a locally running Couchbase instance and opening the default
bucket. The intention is to do a single request to get three different documents based on their key. This
is made possible via the getMulti method in Node.js. If the document keys don’t exist in
Couchbase or there are some other problems, errors will be returned in the results.

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.


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.

One Comment

  1. […] If you look at the EXPLAIN of the above query you’ll notice that no index was used in the query.  The above type of query would be useful if you knew the keys that you wanted to obtain and wanted incredibly fast performance similar to how it was done in a previous article I wrote titled, Getting Multiple Documents by Key in a Single Operation with Node.js. […]

Leave a reply