mongodb/node-mongodb-native · error · MongoCompatibilityError
Current topology does not support sessions
Error message
Current topology does not support sessions
What it means
Thrown when the user explicitly created a ClientSession (session.startSession()) and passed it to an operation, but the topology the connection belongs to does not support sessions. Standalone servers (pre-3.6 or any standalone without replica sets) and very old sharded clusters do not support logical sessions, so the driver rejects the explicit session as a MongoCompatibilityError rather than silently dropping it.
Source
Thrown at src/cmap/connection.ts:398
const { version, strict, deprecationErrors } = this.serverApi;
cmd.apiVersion = version;
if (strict != null) cmd.apiStrict = strict;
if (deprecationErrors != null) cmd.apiDeprecationErrors = deprecationErrors;
}
if (this.hasSessionSupport && session) {
if (
session.clusterTime &&
clusterTime &&
session.clusterTime.clusterTime.greaterThan(clusterTime.clusterTime)
) {
clusterTime = session.clusterTime;
}
const sessionError = applySession(session, cmd, options);
if (sessionError) throw sessionError;
} else if (session?.explicit) {
throw new MongoCompatibilityError('Current topology does not support sessions');
}
// if we have a known cluster time, gossip it
if (clusterTime) {
cmd.$clusterTime = clusterTime;
}
// For standalone, drivers MUST NOT set $readPreference.
if (this.description.type !== ServerType.Standalone) {
if (
!isSharded(this) &&
!this.description.loadBalanced &&
this.supportsOpMsg &&
options.directConnection === true &&
readPreference?.mode === 'primary'
) {
// For mongos and load balancers with 'primary' mode, drivers MUST NOT set $readPreference.
// For all other types with a direct connection, if the read preference is 'primary'View on GitHub (pinned to 3366c21a63)
Solutions
- Connect to a replica set or sharded cluster instead of a standalone if you need sessions/transactions.
- Stop passing an explicit session for operations against a standalone.
- For local dev, start mongod with --replSet and initialize the replica set.
- Ensure the server version is >= 3.6.
Example fix
// before (directConnection to standalone)
const client = new MongoClient('mongodb://localhost:27017/?directConnection=true');
const s = client.startSession();
await coll.find({}, { session: s });
// after
const client = new MongoClient('mongodb://localhost:27017/?replicaSet=rs0'); Defensive patterns
Strategy: validation
Validate before calling
import { MongoClient } from 'mongodb';
async function topologySupportsSessions(uri: string): Promise<boolean> {
const c = new MongoClient(uri);
try {
await c.connect();
const hello = await c.db('admin').command({ hello: 1 });
return Boolean(hello.setName || hello.msg === 'isdbgrid');
} finally {
await c.close().catch(() => {});
}
} Type guard
function deploymentSupportsSessions(kind: 'standalone' | 'replicaset' | 'sharded'): boolean {
return kind !== 'standalone';
} Try / catch
import { MongoCompatibilityError } from 'mongodb';
try {
const s = client.startSession();
await collection.findOne({}, { session: s });
} catch (e) {
if (e instanceof MongoCompatibilityError && /sessions/.test(e.message)) {
// retry without an explicit session, or point at a replica set
}
throw e;
} Prevention
- Use a replica set or sharded cluster if your code needs sessions/transactions.
- For local dev, init a single-node replica set (mongod --replSet rs0).
- Avoid passing explicit sessions when directConnection=true to a standalone.
When it happens
Trigger: Calling collection.find(..., { session }) or any operation with an explicit session against a standalone mongod. Fires in Connection.prepareCommand (src/cmap/connection.ts:386-399) when this.hasSessionSupport is false and the passed session has explicit === true.
Common situations: Connecting with directConnection=true to a standalone mongod and trying to use transactions or explicit sessions; running tests against a local standalone while app code assumes replica set; server version < 3.6 which predates sessions; topology not yet discovered as replica set / sharded cluster at the moment of the command.
Related errors
- Driver attempted to initialize in load balancing mode, but t
- This MongoDB deployment does not support retryable writes. P
- Transactions are not supported in snapshot sessions
- SRV URI does not support directConnection
- Cannot use srvMaxHosts option with replicaSet
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/416b80ca1f78c471.json.
Report an issue: GitHub.