Skip to main content
Prisma Documentation Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Create a new app with MongoDB

Create a new Prisma ORM project with MongoDB using create-prisma@latest.

This page uses MongoDB. Use PostgreSQL instead.

Create a Prisma ORM app with MongoDB, apply the first migration, and run your first query against seeded data.

bun
bun create prisma@latest --provider mongodb --no-deploy
pnpm
pnpm create prisma@latest --provider mongodb --no-deploy
yarn
yarn create prisma@latest --provider mongodb --no-deploy
npm
npm create prisma@latest -- --provider mongodb --no-deploy

Run this on Node.js 22.18 or newer. The command preselects MongoDB and prompts you for the project name, the template, the contract authoring style (PSL or TypeScript), your package manager, and whether to install agent skills. If you would rather add Prisma ORM to a project by hand, without the generated template, follow Set up Prisma ORM from scratch, which uses PostgreSQL.

Setup gives you the app template, a starter contract, prisma-8.md, project-level Prisma ORM skills for your coding agent, and package scripts for the database steps below. Answer no at the skills prompt, or pass --skills none, to skip the agent skill files; to remove them later, see skills sync and the skills config section. Sample users are seeded automatically the first time the app queries the database, so there is no separate seed step.

A standalone mongod is enough to work through this quickstart. A replica set is only needed for transactions and change streams, and MongoDB Atlas already gives you one. The connection string the scaffold writes assumes a single-node replica set named rs0 on port 27017, so either run one locally or edit the string to point at your standalone server. If you use a standalone mongod, remove replicaSet=rs0 from the connection strings below (keep directConnection=true); a URL that names a replica set only connects to a server that belongs to one.

The scaffold writes a .env with a local replica-set connection string:

.env
DATABASE_URL="mongodb://localhost:27017/mydb?replicaSet=rs0&directConnection=true"

The generated scripts read environment variables directly rather than .env, and the CLI and the app use different variable names: the CLI commands read MONGODB_URL, and the app reads DATABASE_URL. Export both in the shell you work in:

Bash
export MONGODB_URL="mongodb://localhost:27017/mydb?replicaSet=rs0&directConnection=true"
export DATABASE_URL="$MONGODB_URL"

If you use MongoDB Atlas, use the connection string from your Atlas cluster instead.

Create the first migration plan from the starter contract.

bun
bun run migration:plan --name init
pnpm
pnpm run migration:plan --name init
yarn
yarn migration:plan --name init
npm
npm run migration:plan -- --name init

The output reports the planned operations: creating the users and posts collections and a unique index on users.email.

Apply the planned migration to MongoDB.

bun
bun run migrate
pnpm
pnpm run migrate
yarn
yarn migrate
npm
npm run migrate

The output ends with a summary like Applied 3 operation(s) across 1 contract space. If it fails with a connection error, confirm MONGODB_URL is exported in this shell and points at a running MongoDB deployment; if the string still names replicaSet=rs0, that replica set has to exist.

The 1 contract space in the summary is the migration history for your own models. Prisma ORM calls it the app contract space, and its migrations are in migrations/app/.

Start the app and confirm the sample query runs successfully.

bun
bun run dev
pnpm
pnpm run dev
yarn
yarn dev
npm
npm run dev

Use the URL or terminal output shown by your template. You should see the seeded users returned from MongoDB.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu