Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Multiple Documents by Key in a Single Operation With Node.js

DZone's Guide to

Getting Multiple Documents by Key in a Single Operation With Node.js

When it comes to obtaining data, doing a lookup based on a document key will always be faster than querying. But, what happens when you have a handful of keys that you need to look up? Did you know you could batch these keys into a single request? Read on and I'll show you how.

· Web Dev Zone
Free Resource

Make the transition to Node.js if you are a Java, PHP, Rails or .NET developer with these resources to help jumpstart your Node.js knowledge plus pick up some development tips.  Brought to you in partnership with IBM.

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:

var Couchbase = require("couchbase");

var cluster = new Couchbase.Cluster("couchbase://localhost");
var bucket = cluster.openBucket("default", "");

bucket.getMulti(["234adsf", "bab7", "fe3f9921"], function(error, results) {
    if(error) {
        console.log(error + " failed to be retrieved");
    }
    for(var key in results) {
        if(results.hasOwnProperty(key)) {
            if(results[key].error) {
                console.log("`" + key + "`: " + JSON.stringify(results[key]));
            }
        }
    }
    process.exit(0);
});

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.

Learn why developers are gravitating towards Node and its ability to retain and leverage the skills of JavaScript developers and the ability to deliver projects faster than other languages can.  Brought to you in partnership with IBM.

Topics:
request ,couchbase ,keys ,queries ,n1ql ,batch

Published at DZone with permission of Nic Raboy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}