Your question
How can I use the mongodb+srv string on the .env file?
What are you trying to do
I've tried to use that notation since I saw on a previous post that this was running correctly but when I try to connect I get a _failed to connect to server on first connect_
I tried several things
For reference:
Step 3 string: mongodb+srv://user:[email protected]/dbname?retryWrites=true&w=majority
Step 4 string: mongodb+srv://user:[email protected]/dbname?retryWrites=true&w=majority
Appreciate the help.
Documentation feedback
Hmm the getaddrinfo() suggests a problem with DNS look up but I am not certain what is triggering that.
It might be helpful to try using mongodb:// instead of mongodb+srv:// and seeing if that works.
Hmm the
getaddrinfo()suggests a problem with DNS look up but I am not certain what is triggering that.It might be helpful to try using
mongodb://instead ofmongodb+srv://and seeing if that works.
Tried with step 3 and 4 strings and got the same errors, I installed mongodb's official driver like docs said but maybe I'm missing another thing?
Hmm thanks. Just to check, are you literally using the hostname cluster0-server0.gcp.mongodb.net / cluster0-shard-00-01-server0.gcp.mongodb.net or are those placeholders for the actual hostnames?
Hmm thanks. Just to check, are you literally using the hostname
cluster0-server0.gcp.mongodb.net/cluster0-shard-00-01-server0.gcp.mongodb.netor are those placeholders for the actual hostnames?
Placeholders, the real ones are cluster0-gaik0.gcp.mongodb.net and cluster0-shard-00-01-gaik0.gcp.mongodb.net
I did some digging, spin up a cluster on Atlas (mongodb.net) and got the same error.
I got it working if I pass an object (rather than a string) and set "useNewUrlParser" to true.
database: {
type: "mongodb",
useNewUrlParser: true,
url: process.env.DATABASE_URL,
},
I don't have to do this when invoking a connection from the mongodb driver directly, so this seems to be caused by something that TypeORM does with 'mongodb+srv' URLs.
In a future update we can patch this to enable that option by default for mongodb+srv URLs, so you can just pass it as a string.
First, thanks for answering so fast, a true lifesaver. It works but I had to remove the last part of the URL ?retryWrites=true&w=majority because it was being identified like part of the database name. Does it make any difference or the plugin takes care of those additional flags?
Final string url on .env file is mongodb+srv://user:[email protected]/dbName
Oh awesome thanks for confirming!
Doh I can see that same behaviour too. Not sure what to do about that yet!
I think maybe you can pass them to TypeORM as options on the object (maybe in a property called 'extra'?).
We can leave this open and I'll investigate that!
I ran into the same mongo connection issues today, followed the steps in this thread (pass an object instead of the connection string) and got it working. Thanks!
About the extra argument: I got this after a while TypeError: Cannot use 'in' operator to search for 'poolSize' in ?retryWrites=true&w=majority, looks like it is working in my development env without any issue without that extra part of the URL but I hope you can find a solution
I think you should be able to find an answer in the TypeORM docs / issues somewhere, it looks like talking to Atlas hosted DB's (with the mongdb+srv pragma) has come up a few times.
I'll definitely try and see if we can abstract it automatically, to address whatever TypeORM is doing that is wrong.
Can I customize the authentication callback functions without connecting to the Mongo Atlas for current work?
Can I customize the authentication callback functions without connecting to the Mongo Atlas for current work?
Yes.
You don't need to use a database to use the callback functions.
@VivianaGarces Thanks again for the bug report! Have created #328 to track it until we can handle it better by default.
Most helpful comment
I did some digging, spin up a cluster on Atlas (mongodb.net) and got the same error.
I got it working if I pass an object (rather than a string) and set "useNewUrlParser" to true.
I don't have to do this when invoking a connection from the mongodb driver directly, so this seems to be caused by something that TypeORM does with 'mongodb+srv' URLs.
In a future update we can patch this to enable that option by default for
mongodb+srvURLs, so you can just pass it as a string.