Create an Atlas Hosted MongoDB
Intro
While developing an application that requires a MongoDB it makes sense to set up and connect to a hosted MongoDB sooner, rather than later. Although we could technically set up a locally-hosted MongoDB server on our compute, starting with the cloud-hosted ensures that any data, users, etc., created will be there upon deployment.
The most popular service for hosting MongoDB databases, not surprisingly, is MongoDB's own Atlas.
Create an Atlas Account
First you will need to signup for a free account here:
Create a New Cluster
Once logged in, Atlas will request that you create a cluster.
Atlas allows one free cluster per account.
A cluster can contain multiple MongoDB databases - which Atlas refers to as namespaces.
Be sure to select the Cloud Provider & Region nearest to you that shows a FREE TIER AVAILABLE:
Next, in the Cluster Tier section, select the M0 Sandbox
tier:
Finally, you can optionally change the name of the cluster, then click the Create Cluster
button:
It may take several minutes for Atlas to build your cluster.
Add a User for the Cluster
Each cluster must have a user created whose credentials will be provided in the database connection string when connecting to a database.
First click the Security tab:
Click the + ADD NEW USER
button, then enter a username, password, select the Read and write to any database option, then click the Add User
button:
Update the Whitelisted IPs
Atlas has a security feature that allows the databases to be accessed by whitelisted (approved) IP addresses only.
However, you must whitelist all IPs to ease development and deployment of your application.
While still in the Security tab, click IP Whitelist, then click the + ADD IP ADDRESS
button.
In the dialog, first click ALLOW ACCESS FROM ANYWHERE
then click the Confirm
button:
Obtain the Connection String
To obtain the connection string for your .env
file, first click the CONNECT
button:
Select the Connect Your Application option:
Next, ensure that the Node.js driver and latest version is selected. Then click the Copy
button to add the connection string to your clipboard:
Use the Connection String in Your App
You can now paste the connection string in the app's .env
file, assigning it to a DATABASE_URL
environment variable:
DATABASE_URL=mongodb+srv://sei:<password>@sei-w0kys.azure.mongodb.net/test?retryWrites=true
You're almost done, but you need to update the connection string as follows:
- Replace
<password>
with the password of the database user you created earlier. - IMPORTANT The connection string by default connects to a namespace (database) named
admin
(...mongodb.net/admin?retryWrites=true...
). However, theadmin
namespace must be updated to your preferred namespace (database) name. For example, "movies" (...mongodb.net/movies?retryWrites=true...
).
You're good to go!
Connecting with Mongoose
Here's the latest options to include to get rid of the deprecation warnings:
mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
Viewing & Editing Data
FYI, you can use the Atlas app to view and edit data by clicking on the COLLECTIONS
button.