var MongoClient = require('mongodb').MongoClient,
    assert = require('assert');
    /*
    Module to comunicate with mongo
    
    */
    
function saveToCollection(params,callback){
        //[collection,{data}]
        var collectionName = params[0];
        var data = params[1];
        var url = 'mongodb://localhost:27017/test2';
        MongoClient.connect(url, function(err, db) {
            assert.equal(null,err);
            var collection=db.collection(collectionName);
            collection.insert(data, {w: 1}, function(err1, records){
                assert.equal(null,err1);
                //console.log("Record added as "+records[0]._id);
            });
            setImmediate(function() {
                callback();
            });
        db.close();
        })
    }  
    function modifyById(id,collectionName,paramsToModify,callback){
        //[collection,{data}]
        //var collectionName = collection;
        var data = paramsToModify;
        var url = 'mongodb://localhost:27017/test2';
        MongoClient.connect(url, function(err, db) {
            assert.equal(null,err);
            var collection=db.collection(collectionName);
            collection.findAndModify(
              {_id: id}, // query
                [['_id','asc']],  // sort order
              {$set: paramsToModify}, // replacement, replaces only the field "hi"
              {upsert:true}, // options
              function(err, object) {
                  if (err){
                      //console.warn(err.message);  // returns error if no matching object found
                  }else{
                     // console.log(object);
                  }
              });
        
            setImmediate(function() {
                callback();
            });
        db.close();
        })
    }    
    function findById(id,collectionName,callback){
        //[collection,{data}]
        //var collectionName = collection;
        //var data = paramsToModify;
        var url = 'mongodb://localhost:27017/test2';
        MongoClient.connect(url, function(err, db) {
            assert.equal(null,err);
            var collection=db.collection(collectionName);
            collection.findOne({_id:id},function(err1, doc) {
            //assert.equal(null, err1);
            //assert.equal(null, doc);
            //assert.equal(2, doc.b);
                setImmediate(function() {
                callback(err1,doc);
            });   
        db.close();
      });
        
        //db.close();
        })
    }
exports.findById=findById;
exports.modifyById=modifyById;
exports.save=saveToCollection;

@@ -960,32 +960,61 @@
 paramsToModify;%0A
+        //console.log(data);%0A
         var url 
@@ -1652,53 +1652,75 @@
     
- // console.log(object);%0A                  %7D%0A
+setImmediate(function() %7B%0A                        callback();%0A     
     
@@ -1721,32 +1721,33 @@
                 
+ 
 %7D);%0A        %0A   
@@ -1734,33 +1734,32 @@
     %7D);%0A        
-%0A
             setI
@@ -1758,33 +1758,32 @@
     
-setImmediate(function() %7B
+ // console.log(object);
 %0A   
@@ -1791,36 +1791,30 @@
             
-callback();%0A
+  %7D%0A  
             
@@ -1809,32 +1809,37 @@
             %7D);%0A
+    %0A
         db.close

fix wrong place of setImmeditate