Search:

Search all manuals
Search this manual
Manual
Couchbase Developer's Guide 1.8
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
5 Structuring Data
Chapter Sections
Chapters

5.2. Using JSON Documents

JavaScript Object Notation (JSON) is a lightweight data-interchange format which is easy to read and change. JSON is language independent although it uses similar constructs to JavaScript. JSON documents enable you to benefit from all the upcoming Couchbase features, such as indexing and querying; they also to provide logical structure for more complex data and enable you to provide logical connections between different records.

The following are basic data types supported in JSON:

For more information about creating valid JSON documents, please refer to JSON.

When you use JSON documents to represent your application data, you should think about the document as a logical container for information. This involves thinking about how data from your application fits into natural groups. It also requires thinking about the information you want to manage in your application. Doing data modeling for Couchbase Server is a similar process that you would do for traditional relational databases; there is however much more flexibility and you can change your mind later on your data structures. As a best practice, during your data/document design phase, you want to evaluate:

For instance, if you are creating a beer application, you might want particular document structure to represent a beer:

{
	"name":
	"description":
	"category":
	"updated":
}

For each of the keys in this JSON document you would provide unique values to represent individual beers. If you want to provide more detailed information in your beer application about the actual breweries, you could create a JSON structure to represent a brewery:

{
	"name":
	"address":
	"city":
	"state":
	"website":
	"description":
}

Performing data modeling for a document-based application is no different than the work you would need to do for a relational database. For the most part, it can be much more flexible, it can provide a more realistic representation or your application data, and it also enables you to change your mind later about data structure. For more complex items in your application, one option is to use nested pairs to represent the information:

{
	"name":
	"address":
	"city":
	"state":
	"website":
	"description":
	"geo": 
	{
	"location": ["-105.07", "40.59"],
	"accuracy": "RANGE_INTERPOLATED"
	}
	"beers": [ _id4058, _id7628]
}

In this case we added a nested attribute for the geo-location of the brewery and for beers. Within the location, we provide an exact longitude and latitude, as well as level of accuracy for plotting it on a map. The level of nesting you provide is your decision; as long as a document is under the maximum storage size for Couchbase Server, you can provide any level of nesting that you can handle in your application.

In traditional relational database modeling, you would create tables that contain a subset of information for an item. For instance a brewery may contain types of beers which are stored in a separate table and referenced by the beer id. In the case of JSON documents, you use key-values pairs, or even a nested key-value pairs.