最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - mongoose how to send transaction multiple collections - Stack Overflow

programmeradmin1浏览0评论

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 sessionmitTransaction();
        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 .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 sessionmitTransaction();
  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?

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.mitTransaction();
        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./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.mitTransaction();
  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?

Share Improve this question edited Oct 17, 2021 at 17:50 swftowu69 asked Oct 16, 2021 at 14:25 swftowu69swftowu69 3731 gold badge5 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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.mitTransaction();
    } 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

发布评论

评论列表(0)

  1. 暂无评论