Published on

How to create a new MongoDB database

Authors

How to create a new MongoDB database

Click on "Create a New Cluster". Choose the free tier (M0). Select a cloud provider and region. For the best performance, choose a region that's geographically close to your users. Leave the Cluster Name as it is or give it a name that you prefer. Click on "Create Cluster". It might take a few minutes for the cluster to be created. Once your cluster is ready, we need to create a database user:

Click on "Database Access" under the "Security" section. Click on "Add New Database User". Enter a username and password. Make sure to remember these as you'll need them to connect to your database. For the database user privileges, choose "Read and write to any database". Click on "Add User". Next, we need to whitelist our IP address:

Click on "Network Access" under the "Security" section. Click on "Add IP Address". Click on "Allow Access from Anywhere" to add 0.0.0.0/0 to the IP whitelist. This allows connections from any IP address. For production applications, you should restrict the IP addresses that can connect to your database. Click on "Confirm". Finally, we need to get our MongoDB connection string:

Click on "Clusters" in the left-hand menu. Click on "Connect" on your cluster. Choose "Connect your application". Select "Node.js" as your driver and choose the version that matches the version of Node.js installed on your machine. Copy the connection string. We'll use this to connect our Next.js app to our MongoDB database. With MongoDB Atlas set up, we're ready to connect it to our Next.js app.