BulkWrite upsert operation sometimes fails with a regex filter


I’m using many updateOne operations inside a BulkWrite command with a regex filter by name. These operations run inside an Azure function that listens to many batch of events across many instances.

The error I get is a E11000 duplicated key error, but when I execute the same batch in a testing environment I don’t get the same behaviour and the document is updated without problems. This document that it tries to create already exists in DB. There’s no way for me to replicate it to find out the cause.

What could be the reason of these BulkWrite exceptions?

Specs of the system:

  • MongoDB Atlas cluster using the version 5.0
  • Node.js v16.20
  • Mongoose 5.13
const operations = elements.map((element) => {
  const operation = {
    updateOne: {
      filter: {
        shardKey: element.shardKey,
        name: {
          $regex: element.name,
          $options: 'i'
      update: {
        $setOnInsert: {
          shardKey: element.shardKey,
          name: element.name
        $set: {
          lastUpdate: new Date()
      upsert: true,
      setDefaultsOnInsert: true

  return operation;

await Model.bulkWrite(operations, { ordered: false });