When you use documents to represent data, a database schema is optional. The majority of your effort will be creating one or more documents that will represent application data. This document structure can evolve over time as your application grows and adds new features.
In Couchbase Server you do not need to perform data modeling and establish relationships between tables the way you would in a traditional relational database. Technically, every document you store with structure in Couchbase Server has its own implicit schema; the schema is represented in how you organize and nest information in your documents.
While you can choose any structure for your documents, the JSON model in particular will help you organize your information in a standard way, and enable you to take advantage of Couchbase Server's ability to index and query. As a developer you benefit in several ways from this approach:
There are several considerations to have in mind when you design your JSON document:
You can use a type field to group together sets of records. For example, the following JSON document contains a type field with the value beer to indicate that the document represents a beer. A document that represents another kind of beverage would use a different value in the type field, such as ale or cider.
{
"beer_id": "beer_Hoptimus_Prime",
"type": "beer",
"abv": 10,
"category": "North American Ale",
"name": "Hoptimus Prime",
"style": "Double India Pale Ale"
}
Here is another type of document in our application which we use to represent breweries. As in the case of beers, we have a type field we can use now or later to group and categorize our beer producers:
{
"brewery_id": "brewery_Legacy_Brewing_Co",
"type": "brewery",
"name": "Legacy Brewing Co.",
"address": "525 Canal Street, Reading, Pennsylvania, 19601 United States",
"updated": "2010-07-22 20:00:20"
}
What happens if we want to change the fields we store for a brewery? In this case we just add the fields to brewery documents. In this case we decide later that we want to include GPS location of the brewery:
{
"brewery_id": "brewery_Legacy_Brewing_Co",
"type": "brewery",
"name": "Legacy Brewing Co.",
"address": "525 Canal Street, Reading, Pennsylvania, 19601 United States",
"updated": "2010-07-22 20:00:20",
"latitude": -75.928469,
"longitude": 40.325725
}
So in the case of document-based data, we extend the record by just adding the two new fields for latitude and longitude. When we add other breweries after this one, we would include these two new fields. For older breweries we can update them with the new fields or provide programming logic that shows a default for older breweries. The best approach for adding new fields to a document is to perform a compare and swap operation on the document to change it; with this type of operation, Couchbase Server will send you a message that the data has already changed if someone has already changed the record. For more information about compare and swap methods with Couchbase, see Compare and Swap (CAS).
To create relationships between items, we again use fields. In this example we create a logical connection between beers and breweries using the brewery field in our beer document which relates to the ID field in the brewery document. This is analogous to the idea of using a foreign key in traditional relational database design.
This first document represents a beer, Hoptimus Prime:
{
"beer_id": "beer_Hoptimus_Prime",
"type": "beer",
"abv": 10,
"brewery": "brewery_Legacy_Brewing_Co",
"category": "North American Ale",
"name": "Hoptimus Prime",
"style": "Double India Pale Ale"
}
This second document represents the brewery which brews Hoptimus Prime:
{
"brewery_id": "brewery_Legacy_Brewing_Co",
"type": "brewery",
"name": "Legacy Brewing Co.",
"address": "525 Canal Street Reading, Pennsylvania, 19601 United States",
"updated": "2010-07-22 20:00:20",
"latitude": -75.928469,
"longitude": 40.325725
}
In our beer document, the brewery field points to 'brewery_Legacy_Brewery_Co', which is the key for the document that represents the brewery. By using this model of referencing documents within a document, we create relationships between application objects.