MongoDB error: $merge cannot be used in a transaction

Viewed 57

I have a transaction operation and I want to make a merge request into a table (that doesn't have a schema)

This is my implementation but it's not working in transactions, I get: $merge cannot be used in a transaction

await User.aggregate([
  {
    $match: {
      _id: new mongoose.mongo.ObjectID(id),
    },
  },
  {
    $merge: {
      into: 'deleted-users',
    },
  },
]).option({ session });

is there an alternative to do this scenario which is to add a record in a Newley created collection inside a transaction ?

1 Answers

Seeing that merge is Excluding the following stages As we can read in official docs:

The following read/write operations are allowed in transactions: (among others) ... aggregate command. Excluding the following stages: (among others) merge.

And here You have reference to merge and more...

Related