0

I am using mongodb native driver:

"mongodb": "^2.2.33",

And trying to retrieve the name of the database from uri...

I tried something like this:

const Mongodb = require('mongodb')
let mongoUrl = new Mongodb.create(opts.mongo.url);
console.log(mongoUrl.databaseName);

But not working, any solution for this?

2 Answers 2

3

its part of the mongodb API

var MongoClient = require('mongodb').MongoClient

MongoClient.connect('mongodb://localhost:27017/animals', function (err, db) {
  if (err) throw err
   console.log(db)
})
Sign up to request clarification or add additional context in comments.

6 Comments

I am sorry ....could you share what does console.log(db) returns
there is a property in the pastebin json -> databaseName: 'test is that required db name ?
glad...i could help :)
But how can I get it? :) I already do it like this: db.s.databaseName but I need to get it with function...
what does console.log(db.s.databaseName) return ? store it in a session
|
1

With "mongodb": "3.3.3" driver, you can use mongoClient.s.options.dbName

Ideally, should have a getter.

Comments

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.