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.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 table){
		DBCursor allrows = table.find();
		//Get the next number
		return 1 + allrows.count();
	}

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

}

@@ -72,16 +72,40 @@
 eption;%0D
+%0Aimport java.util.Date;%0D
 %0A%0D%0Aimpor
@@ -964,24 +964,365 @@
 %22id%22, id);%0D%0A
+%09%09%09Date date = new Date();%0D%0A%09%09%09date.setMonth(1);%0D%0A%09%09%09sensorData.put(%22month%22, date.getMonth());%0D%0A%09%09%09sensorData.put(%22day%22, date.getDay());%0D%0A%09%09%09sensorData.put(%22hour%22, date.getHours());%0D%0A%09        sensorData.put(%22minute%22, date.getMinutes());%0D%0A%09        sensorData.put(%22second%22, date.getSeconds());%0D%0A%09        sensorData.put(%22timestamp%22, date);%09%09%09%0D%0A
 %09%09%09//Insert 

Update MongoDBDaoImpl.java