Skip to content

MongoDB slow delete with onDelete: SetNull #19169

@boenni23

Description

@boenni23

Bug description

When having a schema with a 1:n relation where you delete the left side. It takes on big collections (>1.Mio documents) long time to delete the document (>1 minute).

How to reproduce

You generate a bad query to check the depending documents

{"params":"[]","duration":12735,"query":"db.Broadcast.aggregate([ { $match: { $expr: { $and: [ { }, { $and: [ { $in: [ \"$productionId\", [ { $literal: ObjectId(\"5f913feb235d8600075e1797\"), }, ], ], }, { $ne: [ \"$productionId\", \"$$REMOVE\", ], }, ], }, ], }, }, }, { $project: { _id: 1, productionId: 1, }, }, ])"}

There need to be a query which can use the normal index in mongodb like
db.Broadcast.find({productionId: ObjectId(\"5f913feb235d8600075e1797\")})

Expected behavior

No response

Prisma information

model Broadcast {
  id String @id @default(auto()) @map("_id") @db.ObjectId

  productionId String?     @db.ObjectId
  production   Production? @relation("ProductionBroadcasts", fields: [productionId], references: [id], onDelete: SetNull, onUpdate: Cascade)
}

model Production {
  id               String                 @id @default(auto()) @map("_id") @db.ObjectId
}
prisma.production.delete({where: {id: "...."}})

Environment & setup

Node: 19.7.0
Mongo: 6.0.5

Prisma Version

PrismaClient: 4.14.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions