-1
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {
  if (err) 
  {
  throw err;
  }
  console.log("Database created!");
  db.close();
});

This is the error I am getting when executing this code in nodejs and trying to create a database, I was going through the tutorial in w3school, please help to solve this error--

    C:\Users\n.patel\Desktop\Promotion\nodejs\application4success>node mymongodb.js

C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\lib\mongo_client.js:415
          throw err
          ^
 MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at null.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:3
29:35)
    at emitOne (events.js:77:13)
    at emit (events.js:169:7)
    at null.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:280
:12)
    at g (events.js:260:16)
    at emitTwo (events.js:87:13)
    at emit (events.js:172:7)
    at Socket.<anonymous> (C:\Users\n.patel\Desktop\Promotion\nodejs\application4success\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:187:49)
    at Socket.g (events.js:260:16)
    at emitOne (events.js:77:13)
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Nayan Patel
  • 1,683
  • 25
  • 27
  • You actually need a MongoDB server to be running. That does not come with the node driver BTW. You probably assumed your `npm install` did that. It did not. Install a server [Install MongoDB Community Edition on Windows](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/) – Neil Lunn Jun 12 '17 at 14:02
  • Make sure you have a `mongod`/`mongodb` process running. – glhrmv Jun 12 '17 at 14:02
  • Thanks Neil and dogui, it worked. – Nayan Patel Jun 13 '17 at 09:52

1 Answers1

0

Make sure that you have Mongo running on on port 27017 use this to verify

mongo localhost:27017
Saubhagya
  • 155
  • 12