mongodb/node-mongodb-native · error · MongoInvalidArgumentError
ClientSession must be from the same MongoClient
Error message
ClientSession must be from the same MongoClient
What it means
executeOperation (src/operations/execute_operation.ts:95) throws a MongoInvalidArgumentError when the ClientSession supplied to an operation was created by a different MongoClient instance (session.client !== client). Sessions are bound to the client that created them because they carry cluster time, server pinning, and transaction state tied to that client's topology.
Source
Thrown at src/operations/execute_operation.ts:95
: client.topology;
// The driver sessions spec mandates that we implicitly create sessions for operations
// that are not explicitly provided with a session.
let session = operation.session;
let owner: symbol | undefined;
if (session == null) {
owner = Symbol();
session = client.startSession({ owner, explicit: false });
} else if (session.hasEnded) {
throw new MongoExpiredSessionError('Use of expired sessions is not permitted');
} else if (
session.snapshotEnabled &&
maxWireVersion(topology) < MIN_SUPPORTED_SNAPSHOT_READS_WIRE_VERSION
) {
throw new MongoCompatibilityError('Snapshot reads require MongoDB 5.0 or later');
} else if (session.client !== client) {
throw new MongoInvalidArgumentError('ClientSession must be from the same MongoClient');
}
operation.session ??= session;
const readPreference = operation.readPreference ?? ReadPreference.primary;
const inTransaction = !!session?.inTransaction();
const hasReadAspect = operation.hasAspect(Aspect.READ_OPERATION);
if (
inTransaction &&
!readPreference.equals(ReadPreference.primary) &&
(hasReadAspect || operation.commandName === 'runCommand')
) {
throw new MongoTransactionError(
`Read preference in a transaction must be primary, not: ${readPreference.mode}`
);
}View on GitHub (pinned to 3366c21a63)
Solutions
- Ensure the session is created by the same MongoClient whose collection/db you are calling the operation on.
- Pass the client explicitly or derive sessions from the client that owns the collection.
- Audit code where multiple MongoClient instances coexist and verify session provenance.
Example fix
// before
const session = clientA.startSession();
await clientB.db('app').collection('users').insertOne({}, { session }); // throws
// after
const session = clientB.startSession();
await clientB.db('app').collection('users').insertOne({}, { session }); Defensive patterns
Strategy: validation
Validate before calling
function assertSessionOwner(session: ClientSession, client: MongoClient): void {
if (session.client !== client) {
throw new Error('Session was created by a different MongoClient');
}
}
// usage:
assertSessionOwner(session, client); Type guard
const sessionBelongsTo = (session: ClientSession, client: MongoClient): boolean => session.client === client;
Prevention
- Derive sessions from the same MongoClient whose collections you use.
- Avoid passing sessions across client boundaries in multi-tenant code.
- Centralize session creation near the client that owns the workload.
When it happens
Trigger: Creating a session with clientA.startSession() and passing it to a collection or operation obtained from clientB (a different MongoClient).
Common situations: Multi-tenant applications with one client per tenant, connection-pool reuse bugs, or refactors that swap MongoClient instances without updating session creation.
Related errors
- Unexpected null session. A cursor creating command should ha
- Unexpected null session. A cursor creating command should ha
- Missing required callback parameter
- Use of expired sessions is not permitted
- Snapshot reads require MongoDB 5.0 or later
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/9a4f2ef0efba36d8.json.
Report an issue: GitHub.