mongodb/node-mongodb-native · error · MongoCompatibilityError

Current topology does not support sessions

Error message

Current topology does not support sessions

What it means

This MongoCompatibilityError is thrown during command preparation when the caller passes an explicit session to a connection whose server/topology does not support sessions (hasSessionSupport is false). The driver only allows explicit sessions when the connected deployment supports them — standalone servers before MongoDB 3.6 or certain direct-connection scenarios lack session support. The guard fires in Connection.prepareCommand before any bytes hit the wire.

Solutions

  1. Upgrade the MongoDB server to 3.6+ (preferably 4.0+ for sessions + transactions).
  2. Do not pass an explicit session for operations against a standalone; let the driver manage implicit sessions only on capable topologies.
  3. If connecting to a replica set, verify the connection string uses the replica set name (replicaSet=...) or that the deployment is actually a replica set, not a standalone.
  4. Remove the options.session argument or guard it with topology support detection.

Example fix

// before
const session = client.startSession();
await coll.findOne({}, { session });

// after (standalone or pre-3.6 server)
await coll.findOne({});
Defensive patterns

Strategy: validation

Validate before calling

// Before using a session, confirm the topology supports sessions.
const isStandalone = client.topology.description.type === 'Single';
if (isStandalone) {
  // do not pass explicit session
  await coll.findOne({});
} else {
  const session = client.startSession();
  await coll.findOne({}, { session });
  await session.endSession();
}

Type guard

// Narrow: only pass session when topology supports it
function supportsSessions(client: MongoClient): boolean {
  const type = client.topology.description.type;
  return type === 'ReplicaSetWithPrimary' || type === 'ReplicaSetNoPrimary' || type === 'Sharded' || type === 'LoadBalanced';
}

Try / catch

try {
  await coll.findOne({}, { session });
} catch (e) {
  if (e instanceof MongoCompatibilityError && /sessions/.test(e.message)) {
    // retry without the session
    await coll.findOne({});
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling any operation (find, insert, aggregate, etc.) with an explicitly-created ClientSession (client.startSession()) against a standalone server or a server reporting wire protocol < 6. Also occurs when directConnection=true points at a pre-3.6 mongod, or when SDAM has not yet promoted the server to a type that advertises sessions.

Common situations: Upgrading the driver while pointing at an old MongoDB instance (< 3.6); using replica-set session code against a standalone; misconfiguring directConnection so the driver sees a standalone; connecting through a very old proxy/gateway that strips hello/ismaster session capabilities.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/416b80ca1f78c471. Report an issue: GitHub.

Appendix: 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 dce7939f86)