Skip to main content

Connect your database (MongoDB)

Connecting your database

To connect your database, you need to set the url field of the datasource block in your Prisma schema to your database connection URL:

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")

In this case, the url is set via an environment variable which is defined in .env:

DATABASE_URL="mongodb+srv://test:[email protected]/myFirstDatabase"

You now need to adjust the connection URL to point to your own database.

The format of the connection URL for your database depends on the database you use. For MongoDB, it looks as follows (the parts spelled all-uppercased are placeholders for your specific connection details):


Here's a short explanation of each component:

  • USERNAME: The name of your database user
  • PASSWORD: The password for your database user
  • HOST: The host where a mongod (or mongos) instance is running
  • PORT: The port where your database server is running (typically 27017 for MongoDB)
  • DATABASE: The name of the database

If you see the following error: Error in connector: SCRAM failure: Authentication failed., you can specify the source database for the authentication by adding ?authSource=admin to the end of the connection string.