# MongoDB (/docs/next/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 Next to an existing MongoDB project.

Location: Next > Add to Existing Project > MongoDB

This guide shows how to add Prisma Next to a project that already uses MongoDB. You will run `prisma-next init`, describe the collections you want to work with, emit the generated artifacts, and run a couple of queries.

Use this path when you already have an application and database. Make sure the app can already reach its MongoDB deployment and runs on Node.js 24 or newer. If you want Prisma Next to create a new app for you, use the [MongoDB quickstart](https://www.prisma.io/docs/next/quickstart/mongodb).

> [!NOTE]
> Prisma Next is in Early Access
> 
> Prisma Next is the next major version of Prisma ORM, available now in Early Access. It’s the cutting-edge version of Prisma ORM and will become the future of Prisma, so we’d love for you to try it, explore what’s new, and [share your feedback in Discord](https://pris.ly/discord).
> 
> If you want to stay on the current generally available version of Prisma ORM, you can continue with [Prisma 7](https://www.prisma.io/docs/getting-started).

For local development, use a replica set. MongoDB Atlas already gives you that.

## 1. Make sure you can run the example script [#1-make-sure-you-can-run-the-example-script]

If your project already runs TypeScript scripts, you can skip this step.

Otherwise, install the script tooling:

  

#### bun

```bash
bun add --dev tsx typescript
```

#### pnpm

```bash
pnpm add --save-dev tsx typescript
```

#### yarn

```bash
yarn add --dev tsx typescript
```

#### npm

```bash
npm install --save-dev tsx typescript
```

Later, `prisma-next init` will also add the Node.js types it needs and make sure the generated Prisma Next files can run as ES modules. If your project already declares `"type": "commonjs"`, Prisma Next leaves that choice alone and prints a warning so you can decide how to wire the generated helper into your app.

## 2. Initialize Prisma Next [#2-initialize-prisma-next]

From the root of your existing project, run:

  

#### bun

```bash
bunx prisma-next init --target mongodb
```

#### pnpm

```bash
pnpm dlx prisma-next init --target mongodb
```

#### yarn

```bash
yarn dlx prisma-next init --target mongodb
```

#### npm

```bash
npx prisma-next init --target mongodb
```

This is the existing-project path. It preselects MongoDB, adds Prisma Next files and package scripts to the app you already have, and does not scaffold a new framework project.

It also adds `prisma-next.md` and project-level Prisma Next skills for Cursor, Claude Code, Codex, and Windsurf so your agent can read the Prisma Next usage, upgrade, and extension-author guidance from the project.

When Prisma Next asks the remaining setup questions:

* choose `PSL`
* keep the default schema path, `prisma/contract.prisma`

## 3. Set your database connection string [#3-set-your-database-connection-string]

Update `.env` with the connection string for the MongoDB deployment your app already uses:

```text title=".env"
DATABASE_URL="mongodb://127.0.0.1:27017/app?replicaSet=rs0"
```

## 4. Describe the collections you want Prisma Next to know about [#4-describe-the-collections-you-want-prisma-next-to-know-about]

This is the key adoption step for MongoDB, because you decide which part of the existing database Prisma Next should model first.

PostgreSQL has `contract infer`. MongoDB does not, so this step is manual.

Open `prisma/contract.prisma` and make it match the collections you want Prisma Next to query first. If your existing database already has `users` and `posts` collections with `email`, `name`, `title`, and `authorId`, the starter contract is already a useful first draft:

```prisma title="prisma/contract.prisma"
// use prisma-next

model User {
  id    ObjectId @id @map("_id")
  email String   @unique
  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")
}
```

You do not need to model every collection on day one. Start with the part of the database you want to read and write first.

## 5. Emit the generated artifacts [#5-emit-the-generated-artifacts]

Once the contract looks right, this step turns it into the generated files the runtime and query APIs use.

Run:

  

#### bun

```bash
bunx prisma-next contract emit
```

#### pnpm

```bash
pnpm dlx prisma-next contract emit
```

#### yarn

```bash
yarn dlx prisma-next contract emit
```

#### npm

```bash
npx prisma-next contract emit
```

This refreshes `prisma/contract.json` and `prisma/contract.d.ts` so the runtime and query APIs are aligned with the contract you just reviewed.

## 6. Run a simple high-level query [#6-run-a-simple-high-level-query]

With the emitted artifacts in place, you can test the higher-level API first and confirm Prisma Next can read the existing collections.

Create a `script.ts` file:

```typescript title="script.ts"
import "dotenv/config";
import { db } from "./prisma/db";

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:

  

#### bun

```bash
bunx tsx script.ts
```

#### pnpm

```bash
pnpm dlx tsx script.ts
```

#### yarn

```bash
yarn dlx tsx script.ts
```

#### npm

```bash
npx tsx script.ts
```

## 7. Run a simple low-level query [#7-run-a-simple-low-level-query]

After the ORM example, this step shows the lower-level MongoDB pipeline builder against the same existing collections.

Replace `script.ts` with this version:

```typescript title="script.ts"
import "dotenv/config";
import { db } from "./prisma/db";

async function main() {
  const runtime = await db.runtime();
  const plan = db.query
    .from("users")
    .match((fields) => fields.email.eq("existing@example.com"))
    .project("email", "name")
    .build();

  const rows = await runtime.execute(plan);
  console.log(rows);

  await db.close();
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});
```

Run it again:

  

#### bun

```bash
bunx tsx script.ts
```

#### pnpm

```bash
pnpm dlx tsx script.ts
```

#### yarn

```bash
yarn dlx tsx script.ts
```

#### npm

```bash
npx tsx script.ts
```

## 8. Next steps [#8-next-steps]

When you change `prisma/contract.prisma`, emit the contract again:

  

#### bun

```bash
bunx prisma-next contract emit
```

#### pnpm

```bash
pnpm dlx prisma-next contract emit
```

#### yarn

```bash
yarn dlx prisma-next contract emit
```

#### npm

```bash
npx prisma-next contract emit
```

You do not need a migration just to read collections that already exist. Use [migration plan](https://www.prisma.io/docs/cli/next/migration-plan) when you want Prisma Next to own a schema change.

## Related pages

- [`PostgreSQL`](https://www.prisma.io/docs/next/add-to-existing-project/postgresql): Add Prisma Next to an existing PostgreSQL project.