5

I am attempting to connect to MongoDB via the package mongoose, but get an error along the lines of MongoError: database name must be a string.

I am using Windows, within the script I am also connecting to 2 other APIs which are both connected. I have tried adding my database name when requiring mongoose and also when connecting (.MyDatabaseName to the end).

Mongoose.connect("mongodb+srv://MyUserName:[email protected]", {useNewUrlParser: true}).EternalsMilitary;

Mongoose.connect.once("open", function() {  
    console.log("Connected To MongoDB");
}).on("error", function(err) {
    console.log("Error Connecting To MongoDB: ", err);
});

It's expected to output connected, but it errors with MongoError: database name must be a string.

1
  • 1
    try this: mongoose.connect('mongodb+srv://MyUserName:[email protected]/', {dbName: 'yourDbName'}); Adding the dbname since you're using the mongodb+srv syntax Commented Dec 19, 2018 at 20:18

3 Answers 3

13

I needed to include the database name inside of the URI string. For example... mongodb+srv://MyUserName:[email protected]/MyDatabaseName This will then point it to that specific database.

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

Comments

6

For whatever reason the new url parser doesn't seem to work with certain URLs.

As a quick fix you can try reverting back to the old one with { useNewUrlParser: false }

5 Comments

I already have a fix, using the new URL parser. Thanks anyway though
@William please do share your fix, if you have time :-)
I apologize, should I put it in this comment section?
@William it's possible to answer your own question, that would be a great :)
connection works, but throws deprecation warning saying it should be true
2

In your URI after where it says mongodb.net put slash and the name of the database for example:

mongoose.connect('mongodb+srv://<username>:<password>@<instance>.mongodb.net/**nameDatabase**', {dbName: "Eclipse"}, {useNewUrlParser: true})

4 Comments

Aside from replying to a question over 2 years old, please don't propagate credentials (those are "live" creds - I tested). I edited them out of your answer (though anyone with enough rep can see them)
I just copied them from the comments above as an example and wanted to explain it in a different way for other people to see this, I didn't think anyone would be offended
Also I'm new and I can see them so anyone can see them
This has nothing to do with being offended. The OP made a mistake, publishing real credentials in a comment (if it was in their original question, I could have easily edited them out). This is something to be aware of in the future, to ensure you never re-broadcast such a thing. As I already said, I took care of it. And as far as who can see your answer's history: only people with several thousand rep points can see the edit history and non-edited username/password (not talking about you, as you are the author of your answer).

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.