Quantcast
Channel: Intel Developer Zone Articles
Viewing all articles
Browse latest Browse all 3384

Enabling IBM* Bluemix* on Intel® Edison using MongoDB* by Compose

$
0
0

This article explains how to establish a connection with IBM* Bluemix* cloud services using Node.js* API. This include creating a Bluemix application, adding a mongoDB* connection, as well as storing and retrieving data.

Create a Bluemix application

  1. Log in to Bluemix console, select DASHBOARD and click CREATE APP.
  2. Click on WEB and select SDK for Node.js. Click CONTINUE.
  3. Give a name for the app and click FINISH.
  4. On the top, you can see the status “Your app is staging”.
  5. Once the staging is done, click on overview on the left panel to view the dashboard.
  6. Now from the application dashboard click ADD A SERVICE OR API.
  7. In the services page, click on the MongoDB by Compose service in the Data and Analytics section.
  8. On the right side, you can see options to enter values for Username, Password, Host and Port.
  9. If you don’t have an account with Compose, you may need to create one. Click Register at Compose.
  10. Once registered, login to Compose.io and create a MongoDB deployment.
  11. Using the default values, click Create Deployment.

    It will take few minutes for the deployment to be created; you can see the status as below.

    Once the deployment is finished, you will be redirected to the getting started page where you can create a database.

  12. Create a database by clicking Add Database at the top right corner. Give a name for the database and click Run.
  13. Add a user for the database in order to gain access to the database using connection string.
  14. Click on Admin Settings to obtain the hostname and port details.
  15. On your Bluemix add service page, enter the details for hostname, port, username and password.
  16. Once you click create, click RESTAGE on the popup window that appears.

    After the restaging is finished, you should see a status that reads "Your app is running." In the top-right corner.

Setting up the Development Environment

Install mongodb npm module into your project.

npm install mongodb

Setup mongodb connection

Create a node reference variable for the module and client object for establishing a database connection.

var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient

Create a mongodb connection

The connect function returns a db object, which contains the collection object. The collection object is used to insert and retrieve data from cloud.

The connection url can be obtained from Bluemix console. Select the MongoDB by Compose Service from application dashboard and click Show Credentials.

You can create the connection uri using these credentials. Form the uri as shown below, to be used in the node application:

mongodb://<user>:<password>@<uri>:<port>/iot-compose?ssl=true

Example:

var uri = mongodb://iot-kona:intel123@aws-us-east-1-portal.11.dblayer.com:27832/iot-compose?ssl=true

Copy the uri under credentials and pass it to connect function

db = MongoClient.connect(uri, function(err, db) {});

Store Data

Data can be stored as JSON objects or an array of JSON objects.

data = {‘sensor-id’ : ‘sens341’, ‘value’ : 65.5}
db = MongoClient.connect(config.url, function(err, db) {
	collection = db.collection(config.db);
        collection.insert(data, function(err, result) {});
});

Query Data

Timestamp based query

 

dataQuery = { "timestamp": { $gt: readQuery.timestamp } };

Sensorid based query

dataQuery = { "sensor_id": { $eq: readQuery.sensor_id } }
Run query
collection = db.collection(self.config.db);
collection.find(dataQuery).toArray( function(err, items) {
if(!err)
console.log(JSON.stringify(items, null, ''));
});

References


Viewing all articles
Browse latest Browse all 3384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>