Retrieving Couchbase views with node.js

Reading time ~1 minute

Couchbase is open source NoSQL for mission-critical systems. If you switch from CouchDB to Couchbase or get started with CouchBase with CouchDB as key-value store, you can use the node-memcache library from Tim Eggert to get and set documents.

The memcache library only handles strings, so you have to use JSON.stringify(data) to add a document to the server and JSON.parse(data) after retrieving one.

Unfortunately, the memcache library doesn't work with design documents and views, so you can use baseview to add and delete design documents and retrieve view data.

npm install baseview

var baseview = require('baseview')('http://127.0.0.1:8092');
baseview.view('design_doc', 'view_name', function(error, data) {
  console.log(error, data);
});

baseview also works with spatial views, to retrieve only documents which have a location within a given bounding box. These bounding boxes (bbox) can be imagined as a rectangle on Google Maps. If the document latitude and longitude are within the rectangle, they are returned by the view, otherwise not.

baseview.spatial('geo', 'points', {bbox: bbox}, function(error, points) {
    console.log(error, points);
  });

To get a bounding box from a given location and radius, there's sparta, a small library for geo calculations.

npm install sparta

sparta = require('sparta')
sparta.boundingBox(41.386549,2.170004,1);

This returns a bbox with the center at Plaza Catalunya, Barcelona, Spain with a radius of 1km.

Europe on Two Wheels

![Iron 883](/images/2015/07/IMG_7368.jpg)## Barcelona (Spain) - Le Dramont, Cote d'Azur (France)> 648km![Le Dramont](/images/2015/07/IMG_...… Continue reading

Updates!

Published on September 10, 2014

HTML5 vs. Native [thoughts]

Published on February 26, 2014