{"id":"416b80ca1f78c471","repo":"mongodb/node-mongodb-native","slug":"current-topology-does-not-support-sessions","errorCode":null,"errorMessage":"Current topology does not support sessions","messagePattern":"Current topology does not support sessions","errorType":"exception","errorClass":"MongoCompatibilityError","httpStatus":null,"severity":"error","filePath":"src/cmap/connection.ts","lineNumber":398,"sourceCode":"      const { version, strict, deprecationErrors } = this.serverApi;\n      cmd.apiVersion = version;\n      if (strict != null) cmd.apiStrict = strict;\n      if (deprecationErrors != null) cmd.apiDeprecationErrors = deprecationErrors;\n    }\n\n    if (this.hasSessionSupport && session) {\n      if (\n        session.clusterTime &&\n        clusterTime &&\n        session.clusterTime.clusterTime.greaterThan(clusterTime.clusterTime)\n      ) {\n        clusterTime = session.clusterTime;\n      }\n\n      const sessionError = applySession(session, cmd, options);\n      if (sessionError) throw sessionError;\n    } else if (session?.explicit) {\n      throw new MongoCompatibilityError('Current topology does not support sessions');\n    }\n\n    // if we have a known cluster time, gossip it\n    if (clusterTime) {\n      cmd.$clusterTime = clusterTime;\n    }\n\n    // For standalone, drivers MUST NOT set $readPreference.\n    if (this.description.type !== ServerType.Standalone) {\n      if (\n        !isSharded(this) &&\n        !this.description.loadBalanced &&\n        this.supportsOpMsg &&\n        options.directConnection === true &&\n        readPreference?.mode === 'primary'\n      ) {\n        // For mongos and load balancers with 'primary' mode, drivers MUST NOT set $readPreference.\n        // For all other types with a direct connection, if the read preference is 'primary'","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/connection.ts#L380-L416","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (directConnection to standalone)\nconst client = new MongoClient('mongodb://localhost:27017/?directConnection=true');\nconst s = client.startSession();\nawait coll.find({}, { session: s });\n\n// after\nconst client = new MongoClient('mongodb://localhost:27017/?replicaSet=rs0');","handlingStrategy":"validation","validationCode":"import { MongoClient } from 'mongodb';\nasync function topologySupportsSessions(uri: string): Promise<boolean> {\n  const c = new MongoClient(uri);\n  try {\n    await c.connect();\n    const hello = await c.db('admin').command({ hello: 1 });\n    return Boolean(hello.setName || hello.msg === 'isdbgrid');\n  } finally {\n    await c.close().catch(() => {});\n  }\n}","typeGuard":"function deploymentSupportsSessions(kind: 'standalone' | 'replicaset' | 'sharded'): boolean {\n  return kind !== 'standalone';\n}","tryCatchPattern":"import { MongoCompatibilityError } from 'mongodb';\ntry {\n  const s = client.startSession();\n  await collection.findOne({}, { session: s });\n} catch (e) {\n  if (e instanceof MongoCompatibilityError && /sessions/.test(e.message)) {\n    // retry without an explicit session, or point at a replica set\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["sessions","topology","compatibility","standalone"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}