How to use Strapi with MongoDB and MongoDB Atlas

How to use Strapi with MongoDB and MongoDB Atlas

Strapi allows to chose database according to our requirements and choice. It is currently supporting four types of databases PostgreSQLMongoDBSQLite, and MariaDB. It is recommended to use –quickstart to quickly create a project with SQLite as a default database. But we can use any database depending on our needs.

In this article, we will discuss how to use strapi with MongoDB and MongoDB Atlas.

I assume that MongoDB is already installed locally and running on your machine.

Crate strapi project with MongoDB

Follow these steps to create a strapi project using the MongoDB database.

  1. Run the following command to create a project

    npm create strapi-app new-project
    Please make sure that MongoDB is running in the background.

  2. Select Custom (manual settings ) using drop down keys

    custom

  3. Using arrow key select mongo

  4. Enter the Database name, Host, srv connection and other configurations and wait for few minutes.

    strapi with mongodb and mongodb Atlas

  5. Once done run project using command strapi-develop and create a user.

  6. Now test by adding some content types. If you don’t know about the content type then refer to this article.

    I am adding content types with name news. Add some fields with the type String, Text, and Image.

  7. Let’s add some dummy data to our content type so we can test whether it is working or not.

  8. I have MongoDB Compass Community with me, so I will utilize it to check our database data. But you can use any other GUI tool of your choice.

    strapi with mongodb and mongodb Atlas

Great, our strapi project is successfully connected with the MongoDB database. Now we will check how we can use it with MongoDB Atlas.

Strapi with MongoDB Atlas

I am assuming that you have already created your Atlas account.

  • Login to your account and click on create a new project.
strapi with mongodb and mongodb Atlas

  • In the next step, you have to create a cluster. Click on Build a Cluster
strapi with mongodb and mongodb Atlas
  • Now choose AWS as a cloud provider and Region
  • Now hit on create cluster button.
strapi with mongodb and mongodb Atlas
  • From the left panel choose Database Access under the SECURITY category.
  • In the next screen click on the ADD NEW USER button.
strapi with mongodb and mongodb Atlas
  • Enter the username and Password.
  • In User Privileges, Section make sure to select Read and write to any database.
  • Click Add User to save.
  • In order to whitelist your IP click on Network Access under the SECURITY tab.
  • Click on the ADD IP ADDRESS button.
strapi
  • From the next pop up click on ADD CURRENT IP ADDRESS or manually enter an IP.
  • Leave the comment label field as empty.
  • Hit on save button.
  • Next, you have to connect with your cluster. From configuration settings click on connect.
connection
  • On the next pop up click on connect your application and then choose Driver as Node.js and version as 2.2.12 or latter.
  • Copy the connection string from the box. It will be something like this.
    mongodb://nauman440:@cluster0-shard-00-00-gaoou.mongodb.net:27017,cluster0-shard-00-01-gaoou.mongodb.net:27017,cluster0-shard-00-02-gaoou.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority.
  • Replace the password with the one you used with the username.
  • Cool. We are almost there.
  • In the next step update the database.json file in the project directory.
    Go to ./config/development/database.json
  • After updating the database file it should look something like this. Make sure to update the password.
strapi with mongodb and mongodb Atlas

All set now our strapi project is connected to MongoDB atlas.

Let’s test Atlas connection

As our connection is successfully created. So let’s test by adding some data. Create a new content type with name news and add some data in it. Once the data is added head over to your cluster and click on collections.

strapi with mongodb and mongodb Atlas

Here you go, in the next screen, all the collections will be listed there. Hence it is successfully tested.

How to connect the AWS S3 bucket with strapi for storing data.

How to create content types and fetch data through API in strapi.

How to install and create a project in Strapi?

Share this post

Post Comment