Sample storage documents

The following are some sample JSON documents which demonstrate some different types of application data which can be stored as JSON in Couchbase Server.

Here is an example of a message document:

{
    "from": "user_512",
    "to": "user_768",
    "text": "Hey, that Beer you recommended is pretty fab, thx!"
    "sent_timestamp":476560
}

The next example is a user profile document. Notice in this case, we have two versions of a user profile; in order to extend the number of attributes for a user, you would just add additional string-values to represent the new fields:

{
    "user_id": 512,
    "name": "Bob Likington",
    "email": "[email protected]",
    "sign_up_timestamp": 1224612317,
    "last_login_timestamp": 1245613101
}

{
    "user_id": 768,
    "name": "Simon Neal",
    "email": "[email protected]",
    "sign_up_timestamp": 1225554317,
    "last_login_timestamp": 1234166701,
    "country": "Scotland",
    "pro_account" true,
    "friends": [512, 666, 742, 1111]
}

In this case we add county, account type, and friends as additional fields to our user profile. To extend our application with new user attributes, we simply start storing additional fields at the document level. Unlike traditional relational databases, there is no need for us to have server downtime, or database migration to a new schema.

To add new data fields, we simply start writing the additional JSON values for that particular transaction. You would also update your application to provide a default value for documents that do not yet have these fields.

This last example provides a sample JSON document to store information about a photo:

{
    "photo_id": "ccbcdeadbeefacee",
    "size": {
        "w": 500,
        "h": 320,
        "unit": "px"
        },
    "exposure: "1/1082",
    "aperture": "f/2.4",
    "flash": false,
    "camera": {
            "name": "iPhone 4S",
            "manufacturer": "Apple",
            }
    "user_id": 512,
    "timestamp": [2011, 12, 13, 16, 31, 07]
}

As we did in the brewery document earlier in this chapter, we nest a set of attributes for the photo size and camera by using JSON syntax.