swftowu69
swftowu69

Reputation: 363

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?

Upvotes: 3

Views: 6171

Answers (1)

Ayzrian
Ayzrian

Reputation: 2465

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

Upvotes: 10

Related Questions