Evolving Data Models

Evolving Data Models

Many applications deal with constantly moving requirements and evolving applications schema is part of their daily life. Unlike relational databases or some of the other NoSQL solutions, Couchbase Server does not require a schema declaration.

As a developer, you can simply evolve your applications without any added administrative action in Couchbase Server. Schema changes do not require scheduling downtime or trigger long running, offline updates to existing data.

To accommodate evolving schemas, Couchbase Server provides a great deal of primitives that make it easy for applications to detect various versions of documents. Query and data access APIs make it easy to adapt to evolving schemas with native handling of empty, null and missing attributes.

For example, a "beer" document may have evolved over time with version 1 and 2. "beer" document version 1 explicitly calls out a "canned" attribute to signify if a canned option is available. Version 2 chooses to use a "bottling" attribute that is an array which lists all the bottling options. This evolution in a relational database may require planning but Couchbase Server can simply house the v1 and v2 side by side without a problem. As a developer, you can simply adapt queries for the older and newer versions of "beer" using clauses like IS MISSING clause.
Query:
SELECT * FROM `beer-sample` 
    WHERE type="beer" AND (
    (`bottling` IS NOT MISSING AND `bottling`=["can","glass"]) OR
    (`bottling` IS MISSING AND canned=true AND glassed=true)
    );

The flexibility enables the schema to evolve and application flexibly without restricting availability of the database environment.