A newer version of this documentation is available.

View Latest

Views in a Schema-less Database

      +
      A schema-less database along with view definitions provide for a flexible document structure.

      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:

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

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

      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:

      {
         "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:

      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 provide 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.