Skill 04 · Prisma Database Setup
Subchapter 4.2
references/mongodb.mdMarkdown3 KBView on GitHub
MongoDB projects should stay on the latest Prisma 6.x release. Do not upgrade a MongoDB app to Prisma 7’s SQL client path.
Use the standard Prisma 6 MongoDB setup with prisma-client-js.
In prisma/schema.prisma:
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}Do not apply the Prisma 7 SQL adapter setup here. MongoDB does not use a SQL @prisma/adapter-* package.
MongoDB models must have a mapped _id field using @id and @map("_id"), usually of type String with auto() and db.ObjectId.
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
name String?
}Relations in MongoDB expect IDs to be db.ObjectId type.
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
author User @relation(fields: [authorId], references: [id])
authorId String @db.ObjectId
}In .env:
DATABASE_URL="mongodb+srv://user:password@cluster.mongodb.net/mydb?retryWrites=true&w=majority"prisma migrate commands do not work.prisma db push to sync indexes and constraints.prisma db pull to generate schema from existing data (sampling).prisma init --datasource-provider mongodb is still implemented in Prisma’s CLI source.prisma-client-js, prisma db push, and new PrismaClient() against a MongoDB replica set.Ensure your MongoDB instance is a Replica Set. Standalone instances do not support transactions. Atlas clusters are replica sets by default.
Ensure fields referencing IDs are decorated with @db.ObjectId if the target is an ObjectID.