5

I found that the latest version of MongoDB nodejs driver introduced MongoClient class which is the first class instance I can get after making a connection. But it doesn't provide a default database instance. Below is the source code.

MongoClient.connect(url, (err, client) => {
    if(err) {
      return null;
    }
    client.db('test');  // how can I know the database name? Do I need to parse the url?
  });

The above code shows how to get the mongo client instance after connection. I need to call client.db to get the database instance. My question is how I know the default database name in the client instance. All I get is the connection url. Do I need to parse the connection url in order to get the connected database which is test in the above example?

I know there is a method db.getName() returnes the database name. But how can I get the db instance without parsing the URL to get the database name from the connection?

5 Answers 5

5

In current mongodb driver. The Db class instance has databaseName property. So getting database name that MongoClient was initialized is simply

const db = mongoClient.db();
const dbName = db.databaseName;

Nodejs driver Db documentation

Sign up to request clarification or add additional context in comments.

1 Comment

This is the right answer in the context of nodeJS with mongodb package.
2

As of MongoDB 4.4 this seems to have changed. You can access the current database name through the databaseName property:

const client = await MongoClient.connect(DB_HOST, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
db = client.db(DB_NAME);
console.log(db.databaseName);

Comments

1

The database name exists inside the client returned from the connection:

db = client.db(client.s.options.dbName);

Verified in version 3.2

I couldn't find anything related in the documentation, so not sure if it's accessible from any version of MongoDB driver or if it will change in future versions.

Comments

0

change your code as below

let db = null;
MongoClient.connect('mongodb://localhost:27017/test', (err, client) => {
    if(err) {
      return null;
    }
    db = client.db('test');
  });

use db.getName();

or change config as below

var MongoClient = require('mongodb').MongoClient
  , Server = require('mongodb').Server;

    var mongoClient = new MongoClient(new Server('localhost', 27017));
    mongoClient.open(function(err, mongoClient) {
      var db1 = mongoClient.db("mydb");

      mongoClient.close();
    });

refer to documentation

6 Comments

Does this mean I need to parse my connection url in order to get the database name mydb?
nope. It's a method to get database name you have connected.
I know the db.getName() returnes the database name. But how can I get the db instance without parsing the database name?
To connect to mongoose you must know the db name in advance. using that db name you can create db instance as var db1 = mongoClient.db("mydb");
My original through is that why we have to parse the URL if we already provide the database in the URL. I don't think the driver API upgrade makes sense in this case. At least it should provide a client.db() return the default database from the connection.
|
0

I'm using the following:

const MongoClient = require('mongodb').MongoClient; // [email protected]
const client = await MongoClient.connect(...);
const stats = await client.stats();
const dbName = stats.db;

The name of the database is in the stats object.

1 Comment

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.