Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Prisma with MongoDB fails when you create a User

Errorprisma.user.create() fails against MongoDB in a Next.js route or server action

Short answer

Prisma's MongoDB connector needs a replica set, because it uses transactions for nested writes. It also needs the id field mapped to _id as an ObjectId, and it has no Migrate support, so you apply the schema with prisma db push. Check those three things in order.

  • Applies to Prisma 6.19, MongoDB replica set, Next.js 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the model and the datasource first. Both should match the MongoDB form shown in the Prisma docs.

    sed -n 1,40p prisma/schema.prisma
  2. 2Match what you see, then jump to its fix

    1. 1A single local mongod works for reads, and writes that Prisma wraps in a transaction fail.The MongoDB server is not a replica setFix
    2. 2The model uses id Int or id String @id without a MongoDB mapping.The id field is not mapped to _id as an ObjectIdFix
    3. 3The schema was never pushed, so the unique index on email does not exist.prisma migrate is used, but MongoDB has no MigrateFix

What does the error look like?

  • prisma.user.create() fails against MongoDB in a Next.js route or server action
  • The User model does not work with the mongodb provider

Cause 1 of 3

The MongoDB server is not a replica set

1Check

The Prisma docs say transactions require a replica set deployment, and that Prisma uses MongoDB transactions internally to avoid partial writes on nested queries. A standalone mongod is not one.

mongosh "mongodb://localhost:27017" --eval "rs.status()"

2Fix

Set replication.replSetName in the mongod config file, restart mongod, then run rs.initiate() once. The MongoDB guide says you only initiate the set once. Keep bindIp on localhost for a local set.

# mongod.conf
replication:
  replSetName: "rs0"
net:
  bindIp: localhost

# in mongosh, once
rs.initiate()

3Verify

rs.status() now lists a member, and prisma.user.create() succeeds.

mongosh "mongodb://localhost:27017" --eval "rs.status()"

Confirmed by mongodb.com

Did this fix it?

Cause 2 of 3

The id field is not mapped to _id as an ObjectId

1Check

MongoDB documents have an _id field. The Prisma docs require mapping it with @map("_id"), and an ObjectId field needs @db.ObjectId. @default(auto()) generates one.

grep -n -B2 -A8 "model User" prisma/schema.prisma

2Fix

Declare the id the MongoDB way and set the provider to mongodb.

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

model User {
  id    String @id @default(auto()) @map("_id") @db.ObjectId
  email String @unique
  name  String?
}

3Verify

Regenerate the client, then create a user.

npx prisma generate

Confirmed by prisma.io

Did this fix it?

Cause 3 of 3

prisma migrate is used, but MongoDB has no Migrate

1Check

The Prisma docs say there are no plans to add Prisma Migrate for MongoDB, and that @unique indexes are managed through db push.

grep -n "migrate" package.json

2Fix

Apply the schema with db push, then generate the client yourself: the CLI docs say db push no longer runs generate from Prisma 7.

npx prisma db push
npx prisma generate

3Verify

Create the same email twice: the second write should be rejected by the unique index.

npx prisma db push

Confirmed by prisma.io

Did this fix it?

How do I stop it coming back?

  • Run a replica set in development as well, not a plain standalone mongod.
  • Copy the id line from the Prisma MongoDB docs for every model.
  • Run prisma db push in your setup script instead of Migrate commands.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.