# Add Prisma ORM to an existing MongoDB database (/docs/prisma-orm/add-to-existing-project/mongodb) > For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL. Add Prisma ORM to an app whose MongoDB database already has collections. Location: Prisma ORM > Add To Existing Project > Add Prisma ORM to an existing MongoDB database This page uses MongoDB. [Use PostgreSQL instead](https://www.prisma.io/docs/prisma-orm/add-to-existing-project/postgresql). On this page you add Prisma ORM to an app whose MongoDB database already has collections. You run `orm init`, write a contract that describes your collections, and run two queries. The contract is the file that holds your models. In earlier Prisma ORM versions, it was `schema.prisma`. Your app must reach its MongoDB database and run on Node.js 22.18 or newer on the 22 line, 24.11 or newer on the 24 line, or 26 or newer ([Supported versions](https://www.prisma.io/docs/orm/supported-versions)). A single `mongod` server is enough. You need a replica set only for transactions and change streams, and MongoDB Atlas already runs one. If your database has no collections yet, follow [Add Prisma ORM and MongoDB to an existing app](https://www.prisma.io/docs/prisma-orm/quickstart/existing-app/mongodb) instead, which creates the collections for you. If you want Prisma ORM to create a new app for you, follow [Create a new app with MongoDB](https://www.prisma.io/docs/prisma-orm/quickstart/mongodb). > [!NOTE] > Using Prisma ORM 7? > > Prisma ORM 8 is a release candidate. Prisma ORM 7 remains fully supported; its docs live at [/orm/v7](https://www.prisma.io/docs/orm/v7) and its setup paths at [/v7/getting-started](https://www.prisma.io/docs/v7/getting-started). > > For what release candidate means, when the final release is expected, and how to stay on version 7, see [Release status](https://www.prisma.io/docs/orm/release-status). ## 1. Install `tsx` [#1-install-tsx] The scripts on this page run with `tsx`. If your project does not have it, install it: #### npm ```bash npm install --save-dev tsx typescript ``` #### pnpm ```bash pnpm add --save-dev tsx typescript ``` #### yarn ```bash yarn add --dev tsx typescript ``` #### bun ```bash bun add --dev tsx typescript ``` ## 2. Initialize Prisma ORM [#2-initialize-prisma-orm] The `orm init` command below changes your `tsconfig.json` and your `package.json`. If you have no `tsconfig.json`, it writes one. If you have one, it turns on `resolveJsonModule`, adds `"node"` to `types`, and may set `module` to `preserve` and `moduleResolution` to `bundler`. [`orm init`](https://www.prisma.io/docs/cli/orm-init) lists the `module` and `moduleResolution` settings it leaves as they are. If your `package.json` has no `"type"` field and nothing under `dependencies` yet, it adds `"type": "module"` (packages under `devDependencies` do not count). If it already lists `dependencies`, or has `"type": "commonjs"`, the command keeps it as it is and prints a warning. If your app runs as CommonJS, for example because it loads files with `require` or because `tsc` compiles it to `require` calls, follow [In a CommonJS project](https://www.prisma.io/docs/cli/orm-init#in-a-commonjs-project) before you start your app again. `tsx` runs the scripts on this page in both kinds of app, so you can finish this page first. From the root of your project, run: #### npm ```bash npx prisma@latest orm init --yes --target mongodb --authoring psl --write-env ``` #### pnpm ```bash pnpm dlx prisma@latest orm init --yes --target mongodb --authoring psl --write-env ``` #### yarn ```bash yarn dlx prisma@latest orm init --yes --target mongodb --authoring psl --write-env ``` #### bun ```bash bunx prisma@latest orm init --yes --target mongodb --authoring psl --write-env ``` `prisma@latest` runs Prisma ORM 8, and `--target mongodb` picks MongoDB. `--authoring psl` picks PSL, the Prisma Schema Language, which is the `.prisma` file format you know from earlier Prisma ORM versions. `--write-env` writes a `.env` file, unless you already have one. `--yes` accepts the default answer to every question, so the command asks nothing. The command installs the Prisma ORM packages and writes its files. You use three of them on this page: - `src/prisma/contract.prisma`, an example contract. In step 4 you change it to describe your collections. - `src/prisma/db.ts`, the file your code imports to run queries. - `.env`, which holds the connection string. The command also writes `prisma-8.md`, a short reference for writing queries. You do not need it for this page. From now on, every `prisma` command ends with the line `Prisma agent skills are out of date`. You can ignore it, because it does not change what the command does. Agent skills are instruction files that AI coding tools read, and the line appears because your project has none yet. To add them and stop the line, run [`npx prisma@latest init`](https://www.prisma.io/docs/cli/init). In Prisma ORM 8, `init` adds the skills and a `postinstall` script that keeps them up to date, and it leaves your contract and your `.env` alone. `orm init` is the command that sets up Prisma ORM. ## 3. Set your database connection string [#3-set-your-database-connection-string] If you had no `.env`, `orm init` wrote one with a placeholder, `DATABASE_URL="mongodb://user:password@localhost:27017/mydb"`. Set `DATABASE_URL` in `.env` to the connection string of the database your app already uses: ```text title=".env" DATABASE_URL="mongodb://username:password@host:27017/database" ``` The part after the last `/`, and before any `?`, is the name of the database. To work against a copy on your machine instead, [A database on your machine](https://www.prisma.io/docs/prisma-orm/local-database#mongodb) shows how to run MongoDB in Docker. `orm init` wrote `src/prisma/db.ts`, and you do not need to change it: ```typescript title="src/prisma/db.ts" import 'dotenv/config'; import mongo from '@prisma/orm-mongo/runtime'; import { contractFromJson } from '@prisma/orm-mongo/static'; import type { Contract } from './contract.d.js'; import contractJson from './contract.json' with { type: 'json' }; export const contract = contractFromJson(contractJson); export const db = mongo({ contract, url: process.env['DATABASE_URL']!, }); ``` The first line loads `.env`, so every file that imports `db` reads `DATABASE_URL` from there. The two `contract` files it imports, `contract.json` and `contract.d.ts`, are already there, because `orm init` emitted them from its starter contract. `contract emit` in step 5 writes them again for your own contract. ## 4. Describe your collections in the contract [#4-describe-your-collections-in-the-contract] Prisma ORM cannot read a MongoDB database to write the contract for you, so you write it yourself. Describe only the collections that your code reads and writes through Prisma ORM. The other collections stay as they are. The example contract in `src/prisma/contract.prisma` describes a `users` collection and a `posts` collection: ```prisma title="src/prisma/contract.prisma" // use prisma-8 model User { id ObjectId @id @map("_id") email String @unique username String? name String? posts Post[] @@map("users") } model Post { id ObjectId @id @map("_id") title String content String? author User @relation(fields: [authorId], references: [id]) authorId ObjectId @@map("posts") } ``` Keep the first line, because `contract emit` reads only `.prisma` files that start with it. `@@map("users")` names the collection that stores the model. MongoDB stores the identifier of every document in `_id`, and `@map("_id")` stores the `id` field there. In `db.orm` queries and in their results you use the field name, `id`, and Prisma ORM reads and writes `_id` for you. The same goes for any other field with `@map`. Change the models to match your documents: one model for each collection, with one field for each field of its documents. [Data modeling](https://www.prisma.io/docs/orm/data-modeling) lists the field types. ## 5. Generate the files your code imports [#5-generate-the-files-your-code-imports] `contract emit` takes the place of `prisma generate`, and you run it after every change to the contract: #### npm ```bash npx prisma contract emit ``` #### pnpm ```bash pnpm prisma contract emit ``` #### yarn ```bash yarn prisma contract emit ``` #### bun ```bash bunx prisma contract emit ``` The command writes `src/prisma/contract.json` and `src/prisma/contract.d.ts`, and `db.ts` imports both of them. Commit them, because your app cannot run without them. ## 6. Query a collection with `db.orm` [#6-query-a-collection-with-dborm] `db.orm` queries your models, the way Prisma Client did in earlier Prisma ORM versions. On MongoDB you reach a model by the name of its collection, which is the name in `@@map`. So the `User` model is `db.orm.users`. Create `script.ts` with the code below, and put your own names in it: replace `users` with the name of one of your collections, and replace `email` and `existing@example.com` with a field and a value from its documents. ```typescript title="script.ts" import { db } from "./src/prisma/db.js"; async function main() { const user = await db.orm.users.where({ email: "existing@example.com" }).first(); console.log(user); await db.close(); } main().catch((error) => { console.error(error); process.exit(1); }); ``` Run it: #### npm ```bash npx tsx script.ts ``` #### pnpm ```bash pnpm dlx tsx script.ts ``` #### yarn ```bash yarn dlx tsx script.ts ``` #### bun ```bash bunx tsx script.ts ``` ```text no-copy { username: null, email: 'existing@example.com', id: '6b001f5bb9fd640536ea8896', name: 'Existing User' } ``` The document has no `username` field, and `username` is optional in the contract, so the result shows it as `null`. If no document matches, the script prints `null`. ## 7. Query a collection with a pipeline [#7-query-a-collection-with-a-pipeline] `db.query` builds a MongoDB aggregation pipeline on a collection, which it names as it is stored in MongoDB. `.build()` returns the pipeline, and `runtime.query()` runs it, where `runtime` is the object that `await db.runtime()` returns. Replace `users`, `email`, and the value with your own again. A pipeline reads documents as MongoDB stores them, so it uses stored names, not the field names in your schema. Write `_id`, not `id`, and use the name in `@map` for every other field that has one. Replace `script.ts` with this version: ```typescript title="script.ts" import { db } from "./src/prisma/db.js"; async function main() { const runtime = await db.runtime(); const query = db.query .from("users") .match((fields) => fields.email.eq("existing@example.com")) .project("email", "name") .build(); const rows = await runtime.query(query); console.log(rows); await db.close(); } main().catch((error) => { console.error(error); process.exit(1); }); ``` Run it again: #### npm ```bash npx tsx script.ts ``` #### pnpm ```bash pnpm dlx tsx script.ts ``` #### yarn ```bash yarn dlx tsx script.ts ``` #### bun ```bash bunx tsx script.ts ``` ```text no-copy [ { email: 'existing@example.com', name: 'Existing User', _id: '6b001f5bb9fd640536ea8896' } ] ``` ## 8. Next steps [#8-next-steps] When you change `src/prisma/contract.prisma`, run `npx prisma contract emit` again. You can read and write documents in collections that already exist without a migration. To let Prisma ORM create or change collections and indexes, first bring your development database in line with the contract with [`db update`](https://www.prisma.io/docs/cli/db-update). Do this before your first `migration plan`. `db update` compares the database with the contract and applies the difference. It creates the collections and indexes the contract declares and the database lacks. It also adds a validator to each collection the contract describes. A validator is a MongoDB rule that lists the fields a document may have. > [!WARNING] > Other code that writes to these collections > > Once a collection has a validator, MongoDB rejects any write that adds a field the contract does not declare, including writes from code that does not use Prisma ORM. Declare every field your other code writes before you run `db update`. See the operations first, without applying them: #### npm ```bash npx prisma db update --dry-run ``` #### pnpm ```bash pnpm prisma db update --dry-run ``` #### yarn ```bash yarn prisma db update --dry-run ``` #### bun ```bash bunx prisma db update --dry-run ``` Then apply them: #### npm ```bash npx prisma db update --advance-ref db ``` #### pnpm ```bash pnpm prisma db update --advance-ref db ``` #### yarn ```bash yarn prisma db update --advance-ref db ``` #### bun ```bash bunx prisma db update --advance-ref db ``` Before it adds a validator, `db update` checks the documents already in the collection. If one has a field the contract does not declare, or lacks a required one, the command stops with `MIGRATION.PRECHECK_FAILED`, names the document, and leaves that collection's validator unchanged. Add the field to the contract, or change the document, and run the command again. Operations that ran before the failure, such as creating a missing collection, stay applied. When it succeeds, `db update` records which contract the database matches, in the database and in the file `migrations/app/refs/db.json`, called the `db` ref. `migration plan` starts from that record. Without it, `migration plan` plans every collection from an empty database, and `db migrate` then stops with `MIGRATION.PRECHECK_FAILED` because the collection already exists. If that happened to you, delete the directory that `migration plan` wrote under `migrations/app/`, run `npx prisma db update --advance-ref db`, and plan again. That plan writes only a baseline, because `db update` has already applied your change. After that, every change follows four steps: edit `src/prisma/contract.prisma`, run `npx prisma contract emit`, run `npx prisma migration plan --name `, and run `npx prisma db migrate`. Commit the `migrations/` directory. Your first `migration plan` writes two directories under `migrations/app/`: a baseline, whose name ends in `_baseline` and which describes the collections as `db update` left them, and your change. ### Production and other databases [#production-and-other-databases] Any other database whose collections already exist, such as production, needs the same first step before `db migrate` can run there. Pass that database's connection string with `--db`: `db migrate` then leaves the `db` ref alone, because the ref tracks your development database. If you have not planned a migration yet, run: #### npm ```bash npx prisma db update --db "$PRODUCTION_DATABASE_URL" ``` #### pnpm ```bash pnpm prisma db update --db "$PRODUCTION_DATABASE_URL" ``` #### yarn ```bash yarn prisma db update --db "$PRODUCTION_DATABASE_URL" ``` #### bun ```bash bunx prisma db update --db "$PRODUCTION_DATABASE_URL" ``` If you have, bring the database only to the baseline, so that `db migrate` then runs your migrations on it, including any data changes in them. Pass the name of the baseline directory to `--to`: #### npm ```bash npx prisma db update --db "$PRODUCTION_DATABASE_URL" --to _baseline npx prisma db migrate --db "$PRODUCTION_DATABASE_URL" ``` #### pnpm ```bash pnpm prisma db update --db "$PRODUCTION_DATABASE_URL" --to _baseline pnpm prisma db migrate --db "$PRODUCTION_DATABASE_URL" ``` #### yarn ```bash yarn prisma db update --db "$PRODUCTION_DATABASE_URL" --to _baseline yarn prisma db migrate --db "$PRODUCTION_DATABASE_URL" ``` #### bun ```bash bunx prisma db update --db "$PRODUCTION_DATABASE_URL" --to _baseline bunx prisma db migrate --db "$PRODUCTION_DATABASE_URL" ``` Without `--to`, `db update` would bring the database straight to your current contract, and your migrations would never run there. ## Related pages - [Add Prisma ORM to an existing PostgreSQL database](https://www.prisma.io/docs/prisma-orm/add-to-existing-project/postgresql): Add Prisma ORM to an app whose PostgreSQL database already has tables.