Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ This command does a few things:
- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a new Prisma Postgres database (when using `--db` flag)
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

The generated `prisma.config.ts` file looks like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ This command does a few things:

- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

The generated `prisma.config.ts` file looks like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ This command does a few things:

- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

The generated `prisma.config.ts` file looks like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ This command does a few things:

- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

The generated `prisma.config.ts` file looks like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ This command does a few things:

- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

The generated `prisma.config.ts` file looks like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ This command does a few things:

- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

The generated `prisma.config.ts` file looks like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ This command does a few things:

- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

The generated `prisma.config.ts` file looks like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ import NextSteps from '../../_components/_next-steps.mdx'

[MongoDB](https://www.mongodb.com) is a popular NoSQL document database. In this guide, you will learn how to set up a new TypeScript project from scratch, connect it to MongoDB using Prisma ORM, and generate a Prisma Client for easy, type-safe access to your database.

:::warning[Do not upgrade to Prisma ORM v7 if you are using MongoDB]
:::warning[MongoDB support for Prisma ORM v7]

Prisma ORM v7 is not yet compatible with MongoDB. Please use Prisma ORM v6 instead. Support for MongoDB is coming in a future release.
**MongoDB support for Prisma ORM v7 is coming in the near future.** In the meantime, please use **Prisma ORM v6.19** (the latest v6 release) when working with MongoDB.

This guide uses Prisma ORM v6.19 to ensure full compatibility with MongoDB.

:::

## Prerequisites

<Prerequisites />

You also need:

- Node.js installed in your system [with the supported version](/orm/more/upgrade-guides/upgrading-versions/upgrading-to-prisma-6#minimum-supported-nodejs-versions)
- A [MongoDB](https://www.mongodb.com/) database accessible via connection string

## 1. Create a new project
Expand All @@ -35,10 +34,18 @@ You also need:
Install the packages needed for this quickstart:

```terminal
npm install prisma @types/node --save-dev
npm install @prisma/client dotenv
npm install prisma@6.19 @types/node --save-dev
npm install @prisma/client@6.19 dotenv
```

:::info[Why Prisma v6.19?]

This is the latest stable version of Prisma ORM v6 that fully supports MongoDB. MongoDB support for Prisma ORM v7 is coming soon.

You can also install `prisma@6` and `@prisma/client@6` to automatically get the latest v6 release.

:::

Here's what each package does:

- **`prisma`** - The Prisma CLI for running commands like `prisma init`, `prisma db push`, and `prisma generate`
Expand Down Expand Up @@ -94,11 +101,16 @@ npx prisma init --datasource-provider mongodb --output ../generated/prisma

This command does a few things:

- Creates a `prisma/` directory with a `schema.prisma` file containing your database connection and schema models
- Creates a `prisma/` directory with a `schema.prisma` file for your database connection and schema models
- Creates a `.env` file in the root directory for environment variables
- Generates the Prisma Client in the `generated/prisma/` directory
- Creates a `prisma.config.ts` file for Prisma configuration

:::note

Prisma Client will be generated in the `generated/prisma/` directory when you run `npx prisma generate` later in this guide.

:::

The generated `prisma.config.ts` file looks like this:

```typescript file=prisma.config.ts
Expand Down Expand Up @@ -156,7 +168,11 @@ Update your `.env` file with your MongoDB connection string:
DATABASE_URL="mongodb+srv://username:password@cluster.mongodb.net/mydb"
```

Replace with your actual MongoDB connection string.
:::tip

Replace `username`, `password`, `cluster`, and `mydb` with your actual MongoDB credentials and database name. You can get your connection string from [MongoDB Atlas](https://www.mongodb.com/cloud/atlas) or your MongoDB deployment.

:::

## 5. Define your data model

Expand Down Expand Up @@ -194,19 +210,21 @@ model Post {

## 6. Push your schema to MongoDB

Since MongoDB doesn't use migrations, push your schema directly:
MongoDB doesn't support migrations like relational databases. Instead, use `db push` to sync your schema:

```terminal
npx prisma db push
```

This command creates the collections based on your schema.
This command:
- Creates the collections in MongoDB based on your schema
- Automatically generates Prisma Client

Now run the following command to generate the Prisma Client:
:::info

```terminal
npx prisma generate
```
Unlike relational databases, MongoDB uses a flexible schema. The `db push` command ensures your Prisma schema is reflected in your database without creating migration files.

:::

## 7. Instantiate Prisma Client

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import NextSteps from '../../_components/_next-steps.mdx'

[MongoDB](https://www.mongodb.com/) is a popular document-based NoSQL database known for its flexibility, scalability, and developer-friendly features. In this guide, you will learn how to add Prisma ORM to an existing TypeScript project, connect it to MongoDB, introspect your existing database schema, and start querying with type-safe Prisma Client.

:::warning[MongoDB support for Prisma ORM v7]

**MongoDB support for Prisma ORM v7 is coming in the near future.** In the meantime, please use **Prisma ORM v6.19** (the latest v6 release) when working with MongoDB.

This guide uses Prisma ORM v6.19 to ensure full compatibility with MongoDB.

:::

:::tip

If you're migrating to Prisma ORM from Mongoose, see our [Migrate from Mongoose guide](/guides/migrate-from-mongoose).
Expand All @@ -21,7 +29,7 @@ If you're migrating to Prisma ORM from Mongoose, see our [Migrate from Mongoose

In order to successfully complete this guide, you need:

- [Node.js](https://nodejs.org/en/) installed on your machine (see [system requirements](/orm/reference/system-requirements) for officially supported versions)
- [Node.js](https://nodejs.org/en/) installed on your machine (see [system requirements](/orm/more/upgrade-guides/upgrading-versions/upgrading-to-prisma-6#minimum-supported-nodejs-versions) for officially supported versions)
- An existing TypeScript project with a `package.json` file
- Access to a MongoDB 4.2+ server with a replica set deployment. We recommend using [MongoDB Atlas](https://www.mongodb.com/cloud/atlas).

Expand All @@ -44,8 +52,8 @@ If your project contains multiple directories with `package.json` files (e.g., `
Navigate to your existing project directory and install the required dependencies:

```terminal
npm install prisma @types/node --save-dev
npm install @prisma/client dotenv
npm install prisma@6.19 @types/node --save-dev
npm install @prisma/client@6.19 dotenv
```

Here's what each package does:
Expand All @@ -54,9 +62,11 @@ Here's what each package does:
- **`@prisma/client`** - The Prisma Client library for querying your database
- **`dotenv`** - Loads environment variables from your `.env` file

:::info
:::info[Why Prisma v6.19?]

This is the latest stable version of Prisma ORM v6 that fully supports MongoDB. MongoDB support for Prisma ORM v7 is coming soon.

MongoDB does not require a driver adapter like relational databases. Prisma Client connects directly to MongoDB.
You can also install `prisma@6` and `@prisma/client@6` to automatically get the latest v6 release.

:::

Expand Down Expand Up @@ -121,6 +131,7 @@ generator client {

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

Expand All @@ -138,23 +149,23 @@ For MongoDB Atlas, the connection URL format is:
mongodb+srv://USERNAME:PASSWORD@CLUSTER.mongodb.net/DATABASE
```

The [format of the connection URL](/orm/reference/connection-urls) for MongoDB looks as follows (the parts spelled all-uppercased are _placeholders_ for your specific connection details):
Self-hosted MongoDB connection URL format:

```
mongodb://USERNAME:PASSWORD@HOST:PORT/DATABASE
```

Here's a short explanation of each component:
Connection URL components:

- **`USERNAME`**: The name of your database user
- **`PASSWORD`**: The password for your database user
- **`HOST`**: The host where a [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod) (or [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos)) instance is running
- **`PORT`**: The port where your database server is running (typically `27017` for MongoDB)
- **`DATABASE`**: The name of the database
- **`USERNAME`**: Your database user name
- **`PASSWORD`**: Your database user password
- **`HOST`**: The host where [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod) or [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos) is running
- **`PORT`**: The port where your database server is running (typically `27017`)
- **`DATABASE`**: The name of your database

:::tip

If you're using MongoDB Atlas, you need to manually append the database name to the connection URL because the environment link from MongoDB Atlas doesn't contain it.
For MongoDB Atlas, you can manually append the database name to the connection URL, as Atlas doesn't include it by default.

:::

Expand All @@ -176,15 +187,20 @@ Run the following command to introspect your existing database:
npx prisma db pull
```

This command reads the `DATABASE_URL` environment variable, connects to your database, and introspects the database schema. Prisma ORM introspects a MongoDB schema by sampling the data stored in the database and inferring the schema.
This command:
- Reads the `DATABASE_URL` from your `.env` file
- Connects to your MongoDB database
- Samples documents in your collections to infer the schema
- Generates Prisma models in your `schema.prisma` file

![Introspect your database with Prisma ORM](/img/getting-started/prisma-db-pull-generate-schema.png)

After introspection, your Prisma schema will contain models that represent your existing MongoDB collections.

:::info

MongoDB introspection works by sampling documents in your collections. You may need to manually add relation fields using the `@relation` attribute to enable relational queries.
**MongoDB introspection limitations:** Prisma introspects MongoDB by sampling documents. You may need to manually:
- Add relation fields using the `@relation` attribute
- Adjust field types if the sampling didn't capture all variations
- Add indexes and constraints not detected during introspection

:::

Expand Down Expand Up @@ -244,11 +260,11 @@ npx tsx script.ts

## 8. Evolve your schema

MongoDB does not use migrations. To make changes to your database schema:
MongoDB doesn't support migrations like relational databases. Instead, use `db push` to sync schema changes:

### 8.1. Update your Prisma schema file

Update your Prisma schema file to reflect the changes you want to make to your database schema. For example, add a new model:
Modify your Prisma schema file with the changes you want. For example, add a new model:

```prisma file=prisma/schema.prisma
// add-start
Expand Down Expand Up @@ -276,19 +292,19 @@ In MongoDB, the `id` field is mapped to `_id` and uses `@db.ObjectId` type. Rela

:::

### 8.2. Push the changes to your database:
### 8.2. Push the changes to your database

```terminal
npx prisma db push
```

This command will:
- Apply the schema changes to your MongoDB database
- Regenerate Prisma Client
This command:
- Applies schema changes to your MongoDB database
- Automatically regenerates Prisma Client

:::info
:::info[Why `db push` instead of migrations?]

Prisma Migrate is not supported for MongoDB. Use `prisma db push` to sync your schema changes.
MongoDB uses a flexible schema model. Prisma Migrate (which creates migration files) is not supported for MongoDB. Always use `prisma db push` to sync your schema changes.

:::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Initialize a TypeScript project and add the Prisma CLI as a development dependen

```terminal
npm init -y
npm install typescript tsx @types/node --save-dev
npm install typescript tsx @types/node @types/pg --save-dev
```

This creates a `package.json` file with an initial setup for your TypeScript app.
Expand Down Expand Up @@ -173,14 +173,16 @@ Install the required dependencies to use Prisma Postgres:

```terminal
npm install prisma --save-dev
npm install @prisma/client @prisma/adapter-pg dotenv
npm install @prisma/client @prisma/adapter-pg pg dotenv
```

Here's what each package does:

- **`prisma`** - The Prisma CLI for running commands like `prisma migrate` and `prisma generate`
- **`@prisma/client`** - The Prisma Client library for querying your database
- **`@prisma/adapter-pg`** - The [`node-postgres` driver adapter](/orm/overview/databases/postgresql#using-the-node-postgres-driver) that connects Prisma Client to your database
- **`pg`** - The node-postgres database driver
- **`@types/pg`** - TypeScript type definitions for node-postgres
- **`dotenv`** - Loads environment variables from your `.env` file

### 2.4. Create prisma.config.ts
Expand Down
8 changes: 6 additions & 2 deletions content/200-orm/050-overview/500-databases/600-mongodb.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ codeStyle: false

This guide discusses the concepts behind using Prisma ORM and MongoDB, explains the commonalities and differences between MongoDB and other database providers, and leads you through the process for configuring your application to integrate with MongoDB using Prisma ORM.

:::info
:::warning[MongoDB support for Prisma ORM v7]

**MongoDB support for Prisma ORM v7 is coming in the near future.** In the meantime, please use **Prisma ORM v6.19** (the latest v6 release) when working with MongoDB.

To connect Prisma ORM with MongoDB, refer to our [Getting Started documentation](/getting-started/prisma-orm/quickstart/mongodb).
For getting started guides using Prisma ORM v6.19 with MongoDB, see:
- [Quickstart with MongoDB](/getting-started/prisma-orm/quickstart/mongodb)
- [Add to existing MongoDB project](/getting-started/prisma-orm/add-to-existing-project/mongodb)

:::

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: 'Import data from an existing PostgreSQL database'
sidebar_label: 'Import from existing database'
metaTitle: 'Import from existing Postgres database into Prisma Postgres'
metaDescription: 'Learn how to import data from an existing database into Prisma Postgres.'
tocDepth: 3
toc: true
search: true
---

If you have an existing database and want to import your data into Prisma Postgres, you can use one of our guides:

- [Import from PostgreSQL](/getting-started/prisma-postgres/import-from-existing-database-postgresql)
- [Import from MySQL](/getting-started/prisma-postgres/import-from-existing-database-mysql)
Loading
Loading