Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up a data view in CouchDB with Node.js and Cradle


You can query CouchDB for documents by their ID, but of course, most of the time, you'll want to issue more complex queries, such as matching a field in a record against a particular value. CouchDB lets you define views of your data that consist of an arbitrary key in a collection of objects and then the objects derived from the view. When you specify a view, you're specifying two JavaScript functions: a map function that maps keys to items in your collection, and then an optional reduce function that iterates over the keys and values to create a final collection. In this recipe, we'll use the map function of a view to create an index of records by a single field.

How to do it...

Here's how to add a simple view to the database using CouchDB:

db.save('_design/stations', {
  views: {
    byCall: {
      map: function(doc) {
        if (doc.call) {
          emit(doc.call, doc);
        }
      }
    }
  }
});

This defines a single view...