Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

442
Visualizações
MongoDB Node.js Driver 4.0.0: Cursor session id issues in production on Vercel

Upgrading my Vercel-hosted app to the new MongoDB driver (4.0) has introduced sporadic errors like this:

MongoServerError: Cursor session id ([session hash])is not the same as the operation context's session id (none)

I have fiddled with the MongoDB caching and connection script as recommended by Vercel/Next https://github.com/vercel/next.js/blob/canary/examples/with-mongodb/lib/mongodb.js but the issue continues.

I'm not clear enough on what the changes are in the new driver that might be introducing this, but it's fixed by rolling back to 3.6.10.

The issue seems to happen when a request is made more than 30 seconds after an identical query from the same Node.js function.

My db connection code:

import { MongoClient } from "mongodb";

let uri = process.env.MONGODB_URI;
let dbName = process.env.MONGODB_DB;

let cached = global.mongo;

if (!cached) {
  cached = global.mongo = { conn: null, promise: null };
}

export async function connectToDatabase() {
  if (cached.conn) {
    return cached.conn;
  }

  if (!cached.promise) {
    const opts = {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    };

    cached.promise = MongoClient.connect(uri, opts).then((client) => {
      return {
        client,
        db: client.db(dbName),
      };
    });
  }
  cached.conn = await cached.promise;
  return cached.conn;
}

Edit: This bug in the the v4 Node.js driver was fixed in 4.2.1

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I have the same error when using collection.find() with a large collection, and it disappears after I change it to collection.find().limit(100) - just for your reference.

over 4 years ago · Santiago Trujillo Relatório

0

We also faced this error recently and another potential workaround that helped us is explicitly passing session into the query. It's also written in the corresponding ticket. For example, if you use .find, then you can have something like this:

const session = mongoClient.startSession();
try {
  const query = await collection.find({}, { session });
} finally {
  await session.endSession();
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda