{"id":"2c7c66dfb0d451ff","repo":"mongodb/node-mongodb-native","slug":"snapshot-reads-require-mongodb-5-0-or-later","errorCode":null,"errorMessage":"Snapshot reads require MongoDB 5.0 or later","messagePattern":"Snapshot reads require MongoDB 5\\.0 or later","errorType":"exception","errorClass":"MongoCompatibilityError","httpStatus":null,"severity":"error","filePath":"src/operations/execute_operation.ts","lineNumber":93,"sourceCode":"    client.topology == null\n      ? await abortable(autoConnect(client), operation.options)\n      : client.topology;\n\n  // The driver sessions spec mandates that we implicitly create sessions for operations\n  // that are not explicitly provided with a session.\n  let session = operation.session;\n  let owner: symbol | undefined;\n\n  if (session == null) {\n    owner = Symbol();\n    session = client.startSession({ owner, explicit: false });\n  } else if (session.hasEnded) {\n    throw new MongoExpiredSessionError('Use of expired sessions is not permitted');\n  } else if (\n    session.snapshotEnabled &&\n    maxWireVersion(topology) < MIN_SUPPORTED_SNAPSHOT_READS_WIRE_VERSION\n  ) {\n    throw new MongoCompatibilityError('Snapshot reads require MongoDB 5.0 or later');\n  } else if (session.client !== client) {\n    throw new MongoInvalidArgumentError('ClientSession must be from the same MongoClient');\n  }\n\n  operation.session ??= session;\n\n  const readPreference = operation.readPreference ?? ReadPreference.primary;\n  const inTransaction = !!session?.inTransaction();\n\n  const hasReadAspect = operation.hasAspect(Aspect.READ_OPERATION);\n\n  if (\n    inTransaction &&\n    !readPreference.equals(ReadPreference.primary) &&\n    (hasReadAspect || operation.commandName === 'runCommand')\n  ) {\n    throw new MongoTransactionError(\n      `Read preference in a transaction must be primary, not: ${readPreference.mode}`","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/operations/execute_operation.ts#L75-L111","documentation":"executeOperation (src/operations/execute_operation.ts:93) throws a MongoCompatibilityError when a session with snapshot reads enabled (startSession({ snapshot: true })) is used against a server whose wire version is below MIN_SUPPORTED_SNAPSHOT_READS_WIRE_VERSION, i.e. a server older than MongoDB 5.0. Snapshot sessions are a server feature introduced in 5.0 and cannot be emulated by the driver.","triggerScenarios":"Calling client.startSession({ snapshot: true }) and using it against a standalone, replica set, or sharded cluster running MongoDB 4.4 or earlier.","commonSituations":"Upgrading the driver to use snapshot reads while the deployment has not been upgraded to 5.0+, or pointing a snapshot-enabled app at a legacy test/dev instance.","solutions":["Upgrade the MongoDB deployment to 5.0 or later — snapshot reads require it.","If upgrade is not possible, remove the snapshot:true option from startSession.","Verify the deployment version with a hello/buildInfo command to confirm compatibility before enabling the feature."],"exampleFix":"// before\nconst session = client.startSession({ snapshot: true }); // on MongoDB 4.4\n\n// after (upgrade server, or remove snapshot)\nconst session = client.startSession(); // no snapshot on < 5.0\n// or upgrade the deployment to >= 5.0 and keep snapshot:true","handlingStrategy":"validation","validationCode":"// Check server version before enabling snapshot reads\nconst admin = client.db().admin();\nconst buildInfo = await admin.command({ buildInfo: 1 });\nconst major = parseInt(buildInfo.versionArray[0], 10);\nconst snapshotOk = major >= 5;\nconst session = client.startSession(snapshotOk ? { snapshot: true } : {});","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm the deployment is MongoDB 5.0+ before using snapshot reads.","Gate snapshot-enabled code behind a version check.","Upgrade test/dev environments to match production versions."],"tags":["compatibility","snapshot","version","session"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}