Search:

Search all manuals
Search this manual
Manual
Couchbase Server Manual 2.0
Community Wiki and Resources
Download Couchbase Server 2.0
Couchbase Developer Guide 2.0
Client Libraries
Couchbase Server Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
9 Views and Indexes
Chapter Sections
Chapters

9.6. Views in a Schema-less Database

One of the primary advantages of the document-based storage and the use of map/reduce views for querying the data is that the structure of the stored documents does not need to be predeclared, or even consistent across multiple documents.

Instead, the view can cope with and determine the structure of the incoming documents that are stored in the database, and the view can then reformat and restructure this data during the map/reduce stage. This simplifies the storage of information, both in the initial format, and over time, as the format and structure of the documents can change over time.

For example, you could start storing name information using the following JSON structure:

JSON
{
   "email" : "mc@example.org",
   "name" : "Martin Brown"
}

A view can be defined that outputs the email and name:

Javascript
function(doc, meta) 
{
    emit([doc.name, doc.email], null);
}

This generates an index containing the name and email information. Over time, the application is adjusted to store the first and last names separately:

JSON
{
   "email" : "mc@example.org",
   "firstname" : "Martin",
   "lastname" : "Brown"
}

The view can be modified to cope with both the older and newer document types, while still emitting a consistent view:

Javascript
function(doc, meta) 
{
  if (doc.name && (doc.name != null))
  {
    emit([doc.name, doc.email], null);
  } 
  else 
  {
    emit([doc.firstname + " " + doc.lastname, doc.email], null);
  }
}

The schema-less nature and view definitions allows for a flexible document structure, and an evolving one, without requiring either an initial schema description, or explicit schema updates when the format of the information changes.