MongoDB: Updating a unique index to the same value fails

348 Views Asked by At

I have a collection in MongoDB (posts) with a unique index on urlSlug, and am using the Node library to access it. When I go to update a post like this

const data = {urlSlug: "same-as-existing-slug", foo: "some new foo", bar: "some new bar"}

await collection.updateOne({_id: req.params.documentId}, {$set: {...data}}, {upsert: true});

It fails with a duplicate key error on urlSlug. Is this expected behavior, when updating a key that's equal to the existing key? Or am I doing something wrong?

0

There are 0 best solutions below