Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
mongoose how to send transaction multiple collections

Side-Note I connect to DB with the following code:

const mongoose = require('mongoose');

const connectDB = (url) => {
  return mongoose.connect(url);
}

Problem Description: I have two different Collections. Both Operations, findByIdAndUpdate and create must run as an atomic operation. This should be possible with mongoose Transactions.

const registerCustomer = async (req, res) => {
   await CustomerRegistrationCode.findByIdAndUpdate(req.body._id, { used: true });
   const customer = await Customer.create({firstName: req.body.firstName});
}

What I tried:

const registerCustomer = async (req, res) => {
    const session = await mongoose.startSession();
    await session.startTransaction();
    try {
        await CustomerRegistrationCode.findByIdAndUpdate(req.body._id, { used: true }); //updates even though 
        const customer = await Customer.create({ firstName: req.body.firstName });// this line will throw error
        await session.commitTransaction();
        session.endSession();
    } catch (error) {
        console.error('abort transaction');
        await session.abortTransaction();
        session.endSession();
        throw error;
    }
}

Problem The CustomerRegistrationCode Collection gets updated even though the Customer.create method throws an error. How can this be solved?

New approach to understand MongoDB Transactions fails, but this is official code from https://mongoosejs.com/docs/transactions.html

const mongoose = require('mongoose');
const debugMongo = async () => {
  const db = await mongoose.createConnection("mongodb://localhost:27017/mongotest");
  const Customer = db.model('Customer', new mongoose.Schema({ name: String }));
  const session = await db.startSession();
  session.startTransaction();
  await Customer.create([{ name: 'Test' }], { session: session }); //(node:20416) UnhandledPromiseRejectionWarning: MongoServerError: Transaction numbers are only allowed on a replica set member or mongos
  let doc = await Customer.findOne({ name: 'Test' });
  assert.ok(!doc);
  doc = await Customer.findOne({ name: 'Test' }).session(session);
  assert.ok(doc);
  await session.commitTransaction();
  doc = await Customer.findOne({ name: 'Test' });
  assert.ok(doc);
  session.endSession();
}

debugMongo();

At Customer.create an error gets thrown and i don't know why. Does somebody have an minimal working example?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are using the transaction in a wrong way, that is why it does not work.

You need to pass the session object to your operations.

const registerCustomer = async (req, res) => {
    const session = await mongoose.startSession();
    session.startTransaction();
    try {
        await CustomerRegistrationCode.findByIdAndUpdate(req.body._id, { used: true }, { session });
        const customer = await Customer.create({ firstName: req.body.firstName }, { session });
        await session.commitTransaction();
    } catch (error) {
        console.error('abort transaction');
        await session.abortTransaction();
    } finally {
        session.endSession();
    }
}

Also, I have refactored your code a bit.

You can read more about transactions here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda