package com.sensorcomponent.dao.impl;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.sensorcomponent.dao.MongoDBDao;

public class MongoDBDaoImpl implements MongoDBDao {

	private MongoClient client;
	
	public MongoDBDaoImpl(){
		try {
			client = new MongoClient( "localhost" , 27017 );
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	private long getNextId(final DBCollection collection){
		DBCursor cursor = collection.find();
		//Get the next number
		return 1 + cursor.count();
	}

	public int saveSensorData(final String sensorId, final BasicDBObject sensorData) {
		try{
			DB db = client.getDB("sensordatabase");
			DBCollection collection = db.getCollection(sensorId);
			//Set the Next Number as Id
			sensorData.put("id", getNextId(collection));
			//Insert into DB
			collection.insert(sensorData);
			return 1;
		} catch(Exception e){
			System.out.println("Data not saved for - " + sensorId + ", Reason - " + e);
			return 0;
		} finally {
			if(null != client)
				client.close();
		}
	}

}

@@ -608,26 +608,21 @@
 lection 
-collection
+table
 )%7B%0D%0A%09%09DB
@@ -632,27 +632,23 @@
 sor 
-cursor = collection
+allrows = table
 .fin
@@ -695,14 +695,15 @@
 1 + 
-cursor
+allrows
 .cou
@@ -868,26 +868,21 @@
 lection 
-collection
+table
  = db.ge
@@ -893,24 +893,28 @@
 lection(
+%22
 sensor
-I
 d
+ata%22
 );%0D%0A%09%09%09/
@@ -948,50 +948,63 @@
 %0A%09%09%09
-sensorData.put(%22id%22, getNextId(collection)
+long id = getNextId(table);%0D%0A%09%09%09sensorData.put(%22id%22, id
 );%0D%0A
@@ -1031,18 +1031,13 @@
 %0A%09%09%09
-collection
+table
 .ins

Update MongoDBDaoImpl.java