Strapi allows to chose database according to our requirements and choice. It is currently supporting four types of databases PostgreSQL, MongoDB, SQLite, 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.
- Run the following command to create a project
npm create strapi-app new-project
Please make sure that MongoDB is running in the background.
- Select Custom (manual settings ) using drop down keys
- Using arrow key select mongo
- Enter the Database name, Host, srv connection and other configurations and wait for few minutes.
- Once done run project using command strapi-develop and create a user.
- 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.
- Let’s add some dummy data to our content type so we can test whether it is working or not.
- 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.
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.
- In the next step, you have to create a cluster. Click on Build a Cluster
- Now choose AWS as a cloud provider and Region
- Now hit on create cluster button.
- From the left panel choose Database Access under the SECURITY category.
- In the next screen click on the ADD NEW USER button.
- 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.
- 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.
- 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.
- 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.
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.
Here you go, in the next screen, all the collections will be listed there. Hence it is successfully tested.