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

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