The mongodb connection string is one of the first things I have become aware of when getting started with using mongodb as a database solution. Understanding this string is critical not just for the sake of using mongodb locally, but also when it comes to deployment as well as the content of the string will typically differ between those two environments. In this post I will be writing about the mongodb connection string, and relevant must know topics surrounding this when it comes to things like authentication.
Mongdb is a project where the version number matters, in thie post it is worth pointing out that I am using mongodb 4.0, and as of this writing it is the latest version of mongodb.
Here in this section of this post I will be outlinng some examples of a connection string it string literal format. With many mongo clients such as mongoose, and object can be give that will be used to build the connection string, more on that later, for now lets just look at the actual format examples of a connection string.
So a very basic example of a connection string might just use the mongo connection string prefix, and then just localhost for the host, and then the name of the database, and might look like something like this.
When starting out with a simple project, with mognodb running locally, with authentication disabled, and running on the default port, this might work just fine. However when deploying to a server, chances are it will not. Localhost may need to be replaced with an ip address of another node somewhere other than where the connection is being established, the default port will not do as it is blocked by a firewall, and there may be a username and password that may have to be specified.
A slightly more advanced example of this world be to at least add in a port number. By default mongodb might be running on port 27017, at least it is for me. So the above example could also be written as this.
To know what the port number actually is if it is something different depends on the setup. It could be set via the comand line when the mongod process is started, it could be stored in a mongod.cgf file somewhere. In any case that is how the port would be set in the string, much the same way is it would be in the browser when connection to a web server that is running locally.
In many cases authentication may be enabled, and when that is the case the username, and password should be given in the string in order to connect. This can be done by giving the username after the prefix, followed my the password with : placed between them just as with the host port pair, and then have it end with an at symbol before continuing with everything else.