Skip to main content

MongoDB

Learn how to add Prisma ORM to an existing Node.js or TypeScript project by connecting it to your database and generating a Prisma Client for database access. The following tutorial introduces you to Prisma CLI, Prisma Client, and Prisma Introspection.

tip

If you're migrating to Prisma ORM from Mongoose, see our Migrate from Mongoose guide.

Prerequisites

In order to successfully complete this guide, you need:

  • Node.js installed on your machine (see system requirements for officially supported versions)

  • Access to a MongoDB 4.2+ server with a replica set deployment. We recommend using MongoDB Atlas.

    warning

    The MongoDB database connector uses transactions to support nested writes. Transactions requires a replica set deployment. The easiest way to deploy a replica set is with Atlas. It's free to get started.

Make sure you have your database connection URL (that includes your authentication credentials) at hand! If you don't have a database server running and just want to explore Prisma ORM, check out the Quickstart.

See System requirements for exact version requirements.

Set up Prisma ORM

As a first step, navigate into it your project directory that contains the package.json file.

Next, add the Prisma CLI as a development dependency to your project:

npm install prisma --save-dev
note

If your project contains multiple directories with package.json files (e.g., frontend, backend, etc.), note that Prisma ORM is specifically designed for use in the API/backend layer. To set up Prisma, navigate to the appropriate backend directory containing the relevant package.json file and configure Prisma there.

You can now invoke the Prisma CLI by prefixing it with npx:

npx prisma
info

See installation instructions to learn how to install Prisma ORM using a different package manager.

Next, set up your Prisma ORM project by creating your Prisma Schema file with the following command:

npx prisma init --datasource-provider mongodb --output ../generated/prisma

This command does a few things:

  • Creates a new directory called prisma that contains a file called schema.prisma, which contains the Prisma Schema with your database connection variable and schema models.
  • Sets the datasource to MongoDB and the output to a custom location, respectively.
  • Creates the .env file in the root directory of the project, which is used for defining environment variables (such as your database connection)
Using version control?

If you're using version control, like git, we recommend you add a line to your .gitignore in order to exclude the generated client from your application. In this example, we want to exclude the generated/prisma directory.

.gitignore
generated/prisma/

Note that the default schema created by prisma init uses PostgreSQL as the provider. If you didn't specify a provider with the datasource-provider option, you need to edit the datasource block to use the mongodb provider instead:

prisma/schema.prisma
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}