Manually create the Prisma schema

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

Introspection is often used to get an initial version of your data model when adding Prisma to an existing project.

Seeing as Introspection is not currently supported when working with MongoDB, you have to manually mimic your data models structure using the Prisma Schema Language.

Your own data structure will of course be different, but for this tutorial we will assume you are working with the following models: User, Post and Comment.

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

2datasource db {
3 provider = "mongodb"
4 url = env("DATABASE_URL")
7generator client {
8 provider = "prisma-client-js"
9 previewFeatures = ["mongoDb"]
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
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
31model User {
32 id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
33 email String @unique
34 name String?
35 posts Post[]

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 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