Saturday, 6 May 2017

Node.js application gets MongoError connection closed

When I try to connect with node.js application I get:

MongoError: connection 0 to 127.0.0.1:27017 closed

var MongoClient = require('mongodb').MongoClient
 ,format = require('util').format;    
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {     
  if(err) throw err;
  var collection = db.collection('student');     
  collection.insert({id:4,name:shyam}, function(err, docs) {              
    collection.count(function(err, count) {         
      console.log(format("count = %s", count));
    });
    // Locate all the entries using find       
    collection.find().toArray(function(err, results) {
      console.dir(results);
      // Let's close the db
      db.close();
    });
  });
})



via shyamji

No comments:

Post a Comment