> For the complete documentation index, see [llms.txt](https://nodered-dashboards.gitbook.io/node-red-dashboards/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nodered-dashboards.gitbook.io/node-red-dashboards/workshop/intro-to-cloudant-data-base-example.md).

# Intro to Cloudant Data Base Example

In this flow we are going to see how we can add data into the cloudant database as well as read data from the cloudant data base. This will help us in our further flows where we will can model data

## The Cloudant Database&#x20;

When you create a Node-RED instance using the Node-RED starter kit, you are also creating a Cloudant Database Service. This database allows you to store data that you may want to use while you are building out your flows.&#x20;

### How to access the database&#x20;

#### Step 1. Go to your Node-RED service on the IBM Cloud&#x20;

Go back to IBM Cloud and search for your Node-RED service in the dashboard or in the search bar.&#x20;

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGJpmRZ9rkWEBy9OXf%2FScreen%20Shot%202019-08-14%20at%202.10.43%20PM.png?alt=media\&token=36282761-b98a-433a-833b-e533b29b24c8)

Click on your instance of Node-RED and you should see a cloudantNoSQLDB service in your connection's list&#x20;

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGPoktyIJKthvrbT7F%2FScreen%20Shot%202019-08-14%20at%202.31.46%20PM.png?alt=media\&token=06e9d4a8-5836-435a-906d-0285be126b8c)

#### Step 2 . Click on your Cloudant Connection&#x20;

Click on your Cloudant Connection and it should take you to your connection list&#x20;

Click on the Alias of ....  link and that should take you to your Cloudant Service&#x20;

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGQO_1GtkfFQrQ4Ezq%2FScreen%20Shot%202019-08-14%20at%202.38.54%20PM.png?alt=media\&token=baa7478a-393d-4d07-a31c-a025c47dc28f)

#### Step 3. Go to your Cloudant Dashboard&#x20;

Once on your Cloudant account go to Manage and you should be able to see Launch your cloudant dashboard.&#x20;

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGUHiENNv6z4-oPM2o%2FScreen%20Shot%202019-08-14%20at%202.56.16%20PM.png?alt=media\&token=99dd127f-4651-4ddb-be59-a78dda867e56)

Your Cloudant dashboard should include all the database tables you create in Node-RED&#x20;

## Creating a Flow that Reads and Writes to the Cloudant Dashboard&#x20;

### Step 1.  Insert a inject node connected to a Cloudant node. This will write data into the database

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGVZMYdXbcqtfv3Z4x%2FScreen%20Shot%202019-08-14%20at%203.02.00%20PM.png?alt=media\&token=52497ec9-5296-426e-ab79-37870a3fe0dd)

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGVjYqyk-STZtShcpN%2FScreen%20Shot%202019-08-14%20at%203.02.47%20PM.png?alt=media\&token=a1c27e00-a908-40b4-ad8c-88be10bd52f5)

Make sure the inject value is a payload such as:&#x20;

```
{"_id":"record0","mood":"test0}
```

In this payload we are inserting "\_id" and "mood"

In the cloudant data base node,  name your data base and set operation as insert . This way you are inserting value into your db.&#x20;

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGWrfeWW25cDjNd2sy%2FScreen%20Shot%202019-08-14%20at%203.07.41%20PM.png?alt=media\&token=1d2c2a78-cf55-4ff6-be56-12f79a7ac58c)

### Step 2 . Insert inject node that Reads "\_id" value from the database as well as changes the "mood" value&#x20;

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGWL0nfADvv4kITH_k%2FScreen%20Shot%202019-08-14%20at%203.00.16%20PM.png?alt=media\&token=00f672bc-3e5d-4f90-8e6c-0109cae21438)

Insert another inject node and connect to the database node to read "\_id" value&#x20;

![In the inject node you are trying to read for "\_id"](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGXc10sGNjwAVAbhaW%2FScreen%20Shot%202019-08-14%20at%203.10.43%20PM.png?alt=media\&token=f9ff58e2-d113-4c5e-99dc-8a37f7b5c71a)

![In cloudant dashboard node search by "\_id"](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmG6lxuZcKsptyjz_D6%2F-LmGYstnSCipcA0-rJMH%2FScreen%20Shot%202019-08-14%20at%203.16.22%20PM.png?alt=media\&token=37078f37-053e-47b0-8aa5-6a806968405e)

Insert a change node to change value of "mood"  in the data base&#x20;

![In the change node we changed the value of "mood" to Happy ](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmGZDNLvfZAUv_DuDDD%2F-LmGZlP-q1PkKxW2Uyp8%2FScreen%20Shot%202019-08-14%20at%203.20.11%20PM.png?alt=media\&token=52f099eb-54a4-4a72-a1cf-b0e5e49b2caf)

Deploy your flow and in the debug node you should see :&#x20;

![](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmGZDNLvfZAUv_DuDDD%2F-LmG_twtiAJyPG0VMKZi%2FScreen%20Shot%202019-08-14%20at%203.25.17%20PM.png?alt=media\&token=d45506aa-436a-4a33-bd21-4e0cf73142ac)

### Step 3. See Changed values in the database&#x20;

Go to your Cloudant Database Dashboard and find the database you created

![In this example I created database1 ](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmGZDNLvfZAUv_DuDDD%2F-LmGaFNrfOHluSYwEX8b%2FScreen%20Shot%202019-08-14%20at%203.26.45%20PM.png?alt=media\&token=a0fcc8f4-c82a-4d8a-accc-3912cde60d0f)

Click on your database and you should see a record of all your data&#x20;

![In this database I created a record0 ](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmGZDNLvfZAUv_DuDDD%2F-LmGaRKHHNRPpxGfnKrK%2FScreen%20Shot%202019-08-14%20at%203.27.33%20PM.png?alt=media\&token=d9fa244c-20a0-477c-b72a-590741955fb4)

Click on record you recently created in Node-RED to see the changed data Value&#x20;

![In this example I changed mood value to Happy ](https://1168127261-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ll8Hv_n0TDEU2wd8dWK%2F-LmGZDNLvfZAUv_DuDDD%2F-LmGaeFWU-y3i1d9a5FB%2FScreen%20Shot%202019-08-14%20at%203.28.35%20PM.png?alt=media\&token=d3a4f856-a181-4806-b956-e0fb7b6bbd3f)

You should successfully be able to see this values you stored and created in the cloudant database!&#x20;

## Import this flow from here :&#x20;

```
[{"id":"f29d86a3.e756c","type":"tab","label":"Cloudant","disabled":false,"info":""},{"id":"d2b86d3f.99d94","type":"inject","z":"f29d86a3.e756c","name":"Add a named record to a Cloudant DB","topic":"","payload":"{\"id\":\"record0\",\"mood\":\"test0\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":"","x":190,"y":80,"wires":[["634ab55d.88a704"]]},{"id":"634ab55d.88a704","type":"cloudant out","z":"f29d86a3.e756c","name":"","cloudant":"c4e13a43.b53158","database":"Database1","service":"pmistry_nodeRedTest-cloudantNoSQLDB","payonly":true,"operation":"insert","x":890,"y":80,"wires":[]},{"id":"377b5fef.86df78","type":"inject","z":"f29d86a3.e756c","name":"Read a named record from a Cloudant DB","topic":"","payload":"{\"_id\":\"record0\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":"","x":200,"y":160,"wires":[["430a41ad.3a91f8","65abd75c.b4e56"]]},{"id":"430a41ad.3a91f8","type":"cloudant in","z":"f29d86a3.e756c","name":"","cloudant":"","database":"Database1","service":"pmistry_nodeRedTest-cloudantNoSQLDB","search":"_id","design":"","index":"","x":490,"y":160,"wires":[["143768c7.0f0a67","fb2bffe4.92ad78"]]},{"id":"143768c7.0f0a67","type":"debug","z":"f29d86a3.e756c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":260,"wires":[]},{"id":"fb2bffe4.92ad78","type":"change","z":"f29d86a3.e756c","name":"Update a Cloudant value","rules":[{"t":"set","p":"payload.mood","pt":"msg","to":"Happy","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":140,"wires":[["634ab55d.88a704"]]},{"id":"65abd75c.b4e56","type":"debug","z":"f29d86a3.e756c","name":"","active":true,"console":"false","complete":"false","x":480,"y":260,"wires":[]},{"id":"c4e13a43.b53158","type":"cloudant","z":"","host":"2e8ac18e-55eb-4581-95ba-f23e7b9b4038-bluemix.cloudant.com","name":""}]
```
