Mongoose session.abortTransaction 没有回滚操作

Mongoose session.abortTransaction is not rolling back the actions

这里我正在构建租赁 REST API 我想执行事务但它不回滚更改,它没有给出任何错误问题只是更改没有回滚。

const session = await startSession();
const { error } = validateRental(req.body);
if (error?.details[0].message)
    return res.status(400).send(error.details[0].message);
const movie = await Movies.findById(req.body.movieID, null, {
    $session: session,
}); // here i am adding the session
const customer = await Customer.findById(req.body.customerID, null, {
    $session: session,
}); // same
if (!movie || !customer)
    return res.status(400).send("Please check the customer or Movie ID");

try {
    session.startTransaction();
    movie.numberOfStock--;
    customer.numberOfRental++;

    await movie.save();
    await customer.save();
    await session.commitTransaction();
} catch (e) {
    console.log(e);
    await session.abortTransaction();
    res.status(500).send("Internal System error");
} finally {
    session.endSession();
}

花了很多时间后我才知道我的 mongodb 服务器是独立的,为了写入 属性 我们需要转换成副本集。

遵循官方文档

Convert Standalone to a Replica set

要点你应该先关闭你的服务器

我的 OS 是 window 所以 - net stop mongob

对于ios你可以自己找出来