PlanExecutor Error because of Transaction API

Hello everyone,

I am writing a fairly complex aggregation with roughly 18 stages on a MongoDB cluster with mostly sharded collections. I use the rust MongoDB driver to interact with the database. I open a session and run some database commands inside a transaction, which I commit. I then use the same session to run this one big aggregation and get the following error:

{
  kind: Command(
    CommandError {
      code: 6638800,
      code_name: "Location6638800",
      message: "PlanExecutor error during aggregation :: caused by :: Transaction API does not currently support use within operations with shard or database versions without using router commands",
      topology_version: None,
    },
  ),
  labels: {},
  wire_version: Some(25),
  source: None,
}

The rust driver is on version 3.2.5, the MongoDB version is 8.0.9.

The rough shape of the aggregation is

  1. $match
  2. $project
  3. several times
  4. $lookup with $project pipeline (maybe I should add a $match here to hit the correct shard?)
  5. $unwind
  6. `$project
  7. two $group
  8. $merge into a sharded collection

I did not find anything regarding router commands in the official documentation, but maybe I missed it. I explicitly excluded the aggregation from the transaction, as I read in the documentation that $merge is not supported in transactions.

Do you have an idea how to tackle this error?