-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)
3
  • 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 Commented Jun 12, 2017 at 14:02
  • Make sure you have a mongod/mongodb process running. Commented Jun 12, 2017 at 14:02
  • Thanks Neil and dogui, it worked. Commented Jun 13, 2017 at 9:52

1 Answer 1

0

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

mongo localhost:27017
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.