I have a nestjs project using mongoose under the hoood to interact with a mongo db.

The issue I'm having is I can't seem to start a session successfully from the connection object itself. Whenever I try to use the resulting session it will throw either:

MongoError: No transaction started

100% of the time for

  public async get_session(): Promise<ClientSession> {
    const session = await this.connection.startSession();

    return session;
  }

And intermitently, it will throw:

MongoError: Given transaction number 2 does not match any in-progress transactions. The active transaction number is 1

variants for the following implementation:

public get_session(): ClientSession {
    return this.connection.getClient().startSession();
  }

Looking at the docs -- either should work but in practice this does not seem to be the case.

Versions used:

    "mongoose": "^5.11.18",
    "@nestjs/mongoose": "^7.2.3",
    "@nestjs/core": "^7.6.15",
    "typescript": "4.0.6",
0

There are 0 best solutions below