2

As i'm new to api creation in nodejs and i tried to connect mongo db throung nodejs code and i have advance version of mongodb(V4) and nodejs(v10) and i could not able to connect please can any one help me.

my mongodb node code:

// Retrieve
var MongoClient = require('mongodb').MongoClient;

// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
  if(!err) {
    console.log("We are connected");
  }
});

and cmd: commend for db connection in cmd prompt is mongod -- for connection establishement.

Sruthipriyanga
  • 468
  • 4
  • 17
  • what is the error? – 1565986223 May 02 '19 at 07:42
  • { Error: connect ECONNREFUSED 127.0.0.1:27017 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1104:14) errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 27017 } 'errrrrrrrrrrrrr' – Sruthipriyanga May 02 '19 at 07:45
  • 1
    can you access via command line: `mongo` ? – 1565986223 May 02 '19 at 07:50
  • try `MongoClient.connect("mongodb://localhost:27017/exampleDb", {native_parser:true}, function(err, db) {` – Vikash_Singh May 02 '19 at 08:47
  • C:\Program Files\MongoDB\Server\3.4\bin>mongo MongoDB shell version v3.4.1 connecting to: mongodb://127.0.0.1:27017 2019-05-02T14:22:30.061+0530 W NETWORK [main] Failed to connect to 127.0.0.1:27017 after 5000ms milliseconds, giving up. 2019-05-02T14:22:30.062+0530 E QUERY [main] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:234:13 @(connect):1:6 exception: connect failed @1556089774 – Sruthipriyanga May 02 '19 at 08:52
  • i tired but its getting error (node:19032) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.@Vikash Singh – Sruthipriyanga May 02 '19 at 08:54
  • 1
    for windows `mongod.exe` https://docs.mongodb.com/v3.4/tutorial/install-mongodb-on-windows/#run-mongodb-community-edition – 1565986223 May 02 '19 at 08:57
  • i tried with mongod.exe but it does not run for long time it exit with 2019-05-02T14:28:16.278+0530 I CONTROL [initandlisten] now exiting 2019-05-02T14:28:16.278+0530 I CONTROL [initandlisten] shutting down with code:100 – Sruthipriyanga May 02 '19 at 09:00
  • 1
    https://stackoverflow.com/questions/41420466/mongodb-shuts-down-with-code-100 – 1565986223 May 02 '19 at 09:09
  • Thanks alot its working @1556089774 – Sruthipriyanga May 02 '19 at 09:19
  • you could upvote the answer in the link if it helped. :) – 1565986223 May 02 '19 at 09:20

0 Answers0