Database

Guide: Database

Now that you have set up your project, you can start connecting your database. In this tutorial, we will be using MongoDB, but you can use any database you want.

This is essential if you want to have users, manage payments and subscriptions or anything else that requires data to be persisted. We'll use MongoDB for this example but you can use any database that you want.

Connect a database

I recommend using a local MongoDB for development and a hosted MongoDB for production. You can use MongoDB Atlas for this.

  • Create a MongoDB database and get the connection string (if you use a hosted MongoDB make sure that your IP address is allowed to connect to the DB).
  • Add the connection string to MONGODB_URI inside .env.local file. You should also configure a database name by setting DB_NAME.
  • Enjoy your freshly connected database!
Example Env File
# Database
DB_NAME=my-database
MONGODB_URI=mongodb://localhost:27017/${DB_NAME}
NextTutorial: Authentication