Reputation: 12413
I am trying to connect to my local MongoDB Database that is on docker with nodejs. My component are:
.env file
MONGODB_USERNAME = 'accountUser'
MONGODB_PASSWORD = 'password'
MONGODB_HOST = 'mongodb'
MONGODB_DATABASE = 'mydb'
Code:
const uri = `mongodb+srv://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@${process.env.MONGODB_HOST}/${process.env.MONGODB_DATABASE}?retryWrites=true&w=majority`;
console.log(uri);
const client = new MongoClient(uri);
Console Output
mongodb+srv://accountUser:abc123@mongodb/mydb?retryWrites=true&w=majority
Error
MongoParseError: URI does not have hostname, domain name and tld
What can I be doing wrong?
Upvotes: 1
Views: 6725
Reputation: 529
If the username or password includes the following characters:
:
/
?
#
[
]
@
You should simply replace those characters.
Upvotes: 1
Reputation: 13340
How to connect to MongoDB Atlas from a NestJS Application:
Deployment
select Databases
Connect
Connect your application
DRIVER
select Node.js
VERSION
select 2.2.12 or later
As D. SM
said earlier you can't use a URI with +srv
in it and rolling back the driver version is the only way to get a connection string generated without +srv
.
Upvotes: 0
Reputation: 31
1). Go to the database, click edit password and generate a new password.
2). Go to the database, click edit password and give only characters and numbers.
Upvotes: 2
Reputation: 14520
You are trying to use an SRV URI when you should be using an ordirary URI.
SRV URIs have the additional security requirements on hostnames (that it contains 3 components minimum).
Remove +srv
from your URI.
Upvotes: 9