Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!