## Step 4 - Creating a Product Document

Now let's create our first document using Futon through the following steps:

1. Open the mycouchshop database.
2. Click "New Document".
3. Click "Add Field" to begin adding data to the JSON document. Notice how an ID is pre-filled out for you, I would highly advise not changing this. Add key "name" with the value of "Nettuts CouchDB Tutorial One".
4. Make sure you click the tick next to each attribute to save it.
5. Click "Save Document".

Go up a level, back to the database and you should see one document listed with the previous ID as the key and a value beginning with{rev: . This is the JSON document you just created.

## Step 5 - Updating a Document

CouchDB is an append only database -- new updates are appended to the database and do not overwrite the old version. Each new update to a JSON document with a pre-existing ID will add a new revision. This is what the automatically inserted revision key signifies. Follow the steps below to see this in action:

• Viewing the contents of the mycouchshop database, click the only record visible.
• Add another attribute with the key "type" and the value "product".
• Hit "Save Document".

After hitting save, a new revision key should be visible starting with the number 2. Going back a level to the mycouchshop database view, you will still see just one document, this is the latest revision of our product document.

### Revisions

While CouchDB uses revisions internally, try to not lean on it too much. The revisions can be cleaned through Futon quite easily and it is not designed to be used as a revision control system. CouchDB uses the revisions as part of its replication functionality.

## Step 6 - Creating a Document Using cURL

I've already mentioned that CouchDB uses a RESTful interface and the eagle eyed reader would have noticed Futon using this via the console in Firebug. In case you didn't, let's prove this by inserting a document using cURL via the Terminal.

First, let's create a JSON document with the below contents and save it to the desktop calling the file person.json.

Next, open the terminal and execute cd ~/Desktop/ putting you in the correct directory and then perform the insert with curl -X POST http://127.0.0.1:5984/mycouchshop/ -d @person.json -H "Content-Type: application/json". CouchDB should have returned a JSON document similar to the one below.

This is the ID and revision number of the inserted document. CouchDB follows the RESTful convention and thus:

• POST - creates a new record
• GET - reads records
• PUT - updates a record
• DELETE - deletes a record

## Step 7 - Viewing All Documents

We can further verify our insert by viewing all the documents in our mycouchshop database by executing curl -X GET http://127.0.0.1:5984/mycouchshop/_all_docs.

## Step 8 - Creating a Simple Map Function

Viewing all documents is fairly useless in practical terms. What would be more ideal is to view all product documents. Follow the steps below to achieve this:

• Within Futon, click on the view drop down and select "Temporary View".
• This is the map reduce editor within Futon. Copy the code below into the map function.
• Click run and you should see the single product we added previously.
• Go ahead and make this view permanent by saving it.

After creating this simple map function, we can now request this view and see its contents over HTTP using the following command curl -X GET http://127.0.0.1:5984/mycouchshop/_design/products/_view/products.

A small thing to notice is how we get the document's ID and revision by default.

## Step 9 - Performing a Reduce

To perform a useful reduce, let's add another product to our database and add a price attribute with the value of 1.75 to our first product.

For our new view, we will include a reduce as well as a map. First, we need to map defined as below.

The above map function simply checks to see if the inputted document is a product and that it has a price. If these conditions have been met, the products price is emitted. The reduce function is below.

The above function takes the prices and returns the sum using one of CouchDB's built in reduce functions. Make sure you check the reduce option in the top right of the results table as you may otherwise be unable to see the results of the reduce. You may need to do a hard-refresh on the page to view the reduce option

## Conclusion

In this tutorial, we took a brief but focused look at CouchDB. We saw the potential power of CouchDB and how easy it is to get started. I'm sure you have plenty of questions at this point so feel free to chime in below. Thank you so much for reading!