JavaScript
MongoDB

Manually create the Prisma schema

Migrations and Introspections not supported
Prisma does currently not support Migrations or Introspections when working with a MongoDB database.

Open the prisma/schema.prisma file and replace the default configurations with the following:

prisma/schema.prisma
1datasource db {
2 provider = "mongodb"
3 url = env("DATABASE_URL")
4}
5
6generator client {
7 provider = "prisma-client-js"
8 previewFeatures = ["mongoDb"]
9}
10
11model Post {
12 id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
13 slug String @unique
14 title String
15 body String
16 comments Comment[]
17 user User @relation(fields: [userId], references: [id])
18 userId String @db.ObjectId
19}
20
21// Comments contain a comment string and connect back to the post.
22// postId must have @db.ObjectId to match up with Post's id type
23model Comment {
24 id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
25 post Post @relation(fields: [postId], references: [id])
26 postId String @db.ObjectId
27 comment String
28}
29
30model User {
31 id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
32 email String @unique
33 name String?
34 posts Post[]
35}

Because MongoDB is currently a preview feature, you need to explicitly define that in the generator block with previewFeatures = ["mongoDb"].

There are also a number of subtle differences in how the schema is setup when compared to relational databases like PostgreSQL.

For example, the underlying ID field name is always _id and must be mapped with @map("_id").

For more information check out the MongoDB schema reference.

Edit this page on GitHub