{"record":{"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/dce7939f86fb283e167ad709955abedb7bf23124/src/cmap/connection.ts#L380-L416","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Upgrade the MongoDB server to 3.6+ (preferably 4.0+ for sessions + transactions).","Do not pass an explicit session for operations against a standalone; let the driver manage implicit sessions only on capable topologies.","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.","Remove the options.session argument or guard it with topology support detection."],"exampleFix":"// before\nconst session = client.startSession();\nawait coll.findOne({}, { session });\n\n// after (standalone or pre-3.6 server)\nawait coll.findOne({});","handlingStrategy":"validation","validationCode":"// Before using a session, confirm the topology supports sessions.\nconst isStandalone = client.topology.description.type === 'Single';\nif (isStandalone) {\n  // do not pass explicit session\n  await coll.findOne({});\n} else {\n  const session = client.startSession();\n  await coll.findOne({}, { session });\n  await session.endSession();\n}","typeGuard":"// Narrow: only pass session when topology supports it\nfunction supportsSessions(client: MongoClient): boolean {\n  const type = client.topology.description.type;\n  return type === 'ReplicaSetWithPrimary' || type === 'ReplicaSetNoPrimary' || type === 'Sharded' || type === 'LoadBalanced';\n}","tryCatchPattern":"try {\n  await coll.findOne({}, { session });\n} catch (e) {\n  if (e instanceof MongoCompatibilityError && /sessions/.test(e.message)) {\n    // retry without the session\n    await coll.findOne({});\n  } else { throw e; }\n}","preventionTips":["Target MongoDB 3.6+ deployments when using sessions.","Detect standalone topology and skip explicit-session code paths.","Prefer implicit sessions managed by the driver on capable topologies."],"tags":["sessions","topology","compatibility","standalone"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}