Should i use Transactions?

Hi,

I’m trying to perform two operations within a transaction but it’s really frustrating to see the Don't use transactions in MongoDB advice everywhere. That makes me overthink everything.

Here if i want to block a user (isBlocked = true), I should also mark their conversation as closed (isClosed = true).

Should i avoid transactions here?

await Promise.all([
    User.updateOne({ _id: data.id }, {
        isBlocked: true,
    }, { session }),

    Conversation.updateOne({ user: data.id }, {
        isClosed: true,
    }, { session }),
]);

I do not think that blocking a user is a very frequent use-case.

So I do not think that any overhead of transaction should be an issue. If you have performance issue then fix it then. Do not worry about performance of uncommon use-case. Make them work correctly and then make them work fast if needed.

From what I see, it looks like a user has only 1 conversation. If not you might want to use updateMany.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.