Mongo db connection not establishing

mongodb
mongodb atlas
mongodb compass
mongodb connection string
npm mongodb
mongonetworkerror: failed to connect to server on first connect
mongodb download
mongodb tutorial

Below is the code which i used to up the server. It is running in the localhost server 3000.

app.use('/', books);

app.listen(3000, function(){
    console.log('Server running at port 3000: http://localhost:3000')
});

Below code throws mongodb error. Dont know what exactly the error. Below is my code

var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://127.0.0.1:27017';

// SHOW LIST OF BOOKS
router.get('/book', function(req, res, next) {  
    MongoClient.connect(url, function(error, db) {
      if (error) throw error;
      var dbo = db.db("BookStore");
      dbo.collection("Books").find().toArray(function(err, result) {
        if (err) throw err;
        res.send(result);
        console.log(result);
        db.close();
      });
    });
});

// SHOW BOOK with ISBN
router.get('/book/:isbn', function(req, res, next) {
    var isbn = parseInt(req.params.isbn);   
    MongoClient.connect(url, function(error, db) {
      if (error) throw error;
      var dbo = db.db("BookStore");
      var query = { "isbn": isbn };
      dbo.collection("Books").findOne(query, function(err, result) {
        if (err) throw err;
        res.send(result);
        console.log(result);
        db.close();
      });
    });
});


// Add BOOK
router.post('/book', function(req, res, next) { 
    MongoClient.connect(url, function(error, db) {
      if (error) throw error;
      var dbo = db.db("BookStore");
      var myobj = {
        "isbn":req.body.isbn,
        "publisher":req.body.publisher,
        "title":req.body.title,
        "authors":req.body.authors,
        "publishedDate":req.body.publishedDate,
        "price":req.body.price
      };
      dbo.collection("Books").insertOne(myobj, function(err, result) {
        if (err) throw err;
        res.send('Book with '+req.body.isbn+' is successfully added');
        db.close();
      });
    });
});

// UPDATE BOOK
router.put('/book/:isbn', function(req, res, next) {
    var isbn = parseInt(req.params.isbn);
    MongoClient.connect(url, function(error, db) {
      if (error) throw error;
      var dbo = db.db("BookStore");
      var query = { "isbn": isbn };
      var newvalues = { $set: {
            "publisher":req.body.publisher,
            "title":req.body.title,
            "authors":req.body.authors,
            "publishedDate":req.body.publishedDate,
            "price":req.body.price
          }
        };
      dbo.collection("Books").updateOne(query, newvalues, function(err, result) {
        if (err) throw err;
        res.send('Book with '+req.params.isbn+' is successfully updated');
        db.close();
      });
    });
});


// DELETE BOOK
router.delete('/book/:isbn', function(req, res, next) {
    var isbn = parseInt(req.params.isbn);
    MongoClient.connect(url, function(error, db) {
      if (error) throw error;
      var dbo = db.db("BookStore");
      var query = { "isbn": isbn };
      dbo.collection("Books").deleteMany(query, function(err, result) {
        if (err) throw err;
        res.send('Book with '+req.params.isbn+' is successfully removed');
        db.close();
      });
    });
});

module.exports = router;

Code Explanation:

We are creating a route to our application called 'Books.' So whenever anybody browses to http://localhost:3000/books of our application, the code snippet defined for this route will be executed.

Here we are getting all of the records in our 'Books' collection through the db.collection('Books').find() command. We are then assigning this collection to a variable called cursor. Using this cursor variable, we will be able to browse through all of the records of the collection.

Run sudo mongod in the terminal

Couldn't connect to server 127.0.0.1:27017, Cluster Connect button is disabled; Connecting IP address not whitelisted; Incorrect To create a database user for your cluster, see Configure Database Users. Mongo db connection not establishing. Ask Question Asked 1 year, 4 months ago. Active 1 year, 4 months ago. How do you query for “is not null” in Mongo? 395.

Is it impossible to connect even if you change the value assigned to the variable url to mongodb://[user_name]:[password]@127.0.0.1:27017/[your_database_name]?authSource=admin ?

Connect to MongoDB, Close ×. MongoDB Manual. Version 4.2 (current). Version 4.4 (upcoming); Version 4.2 (current); Version 4.0; Version 3.6; Version 3.4; Version 3.2; Version 3.0  Optional. A query string that specifies connection specific options as <name>=<value> pairs. See Connection String Options for a full description of these options. If the connection string does not specify a database/ you must specify a slash (/) between the last host and the question mark (?) that begins the string of options.

Actually, the error is related to MongoDB version(MongoClient.connect(url, function(error, db)), MongoDB developers have changed their API. I don't know which version, it throws an error when you use MongoClient.connect(url, function(error, db). So it should be MongoClient.connect(url, function(err, client) Link to api-doc

And do not forget to run mongod

How to setup a local MongoDB Connection, MongoDB connection error: MongoError: failed to connect to server version: "2" services: isawyou-server: build: . environment: Strangely enough, with the the MongoDB Compass I also can not connect. receiving: Overview¶. The MongoDB ODBC Driver for BI Connector provides connectivity between a SQL client and the MongoDB Connector for BI. The ODBC driver enables users to create a Data Source Name (DSN) and connect a variety of BI tools to the BI Connector.

Troubleshoot Connection Issues, I'm trying to add remote MongoDB 3.2.6 (auth by login and to host and review code, manage projects, and build software together. Connect to MongoDB error: Connection to successful, but could not connect to DB. Operation execution is not restored after the service restart. Seems like this is only the issue for ASP.NET apps, hence console app is able to restore connection/operation execution when the service as back online again.

connect(), consider int the future. That is the mongodb connection string. The ones not supported by the Node.js driver are left out for simplicities sake. forceServerObjectId {Boolean, default:false}, force server to create _id fields instead of client. A URI (Uniform Resource Identifier) is similar to a URL, and is supplied as a parameter to the mongo shell, Compass, and the MongoDB drivers when connecting to a MongoDB deployment. Note You can create a URI string for your session in MongoDB Guides by copying and pasting your Atlas connection string in the form below.

MongoDB connection error: MongoError: failed to connect to server , It is not part of the command. Terminal returns 'test' when we run the 'db' command. For this article, we'll create a database called  The following steps describe how to create a system Data Source Name (DSN) for the BI Connector’s mongosqld process. A DSN is a saved configuration which describes a database connection to be used by an ODBC driver. Once the DSN is created for the BI Connector, you can configure a wide range of SQL clients and BI tools to use the DSN and

Comments
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post instead, provide answers that don't require clarification from the asker