1

Is there any way of using transactions in MongoDB 4.0 with mongoose, without using Promises, I want to use Callbacks only?

I read this post post but didn't find anything with Callbacks.

Please help me.

2 Answers 2

1

I would assume that you can do something along the lines:

mongoose.startSession() .then(session => {//do some db operation}) .then(//another one) .then(mongoose.endSession()) .catch(session.abortTransaction());

Please also refer to this question. It's not specific for MongoDB 4.0, but might help.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for telling me, But is it possible to do with only using Callbacks?
Yes @sudhanshu-gaur, you can pass a callback function as the last argument of mongoose.startSession(): mongoosejs.com/docs/api.html#mongoose_Mongoose-startSession
-1

Mongo DB not support Multi-Document Transactions:

When a single write operation (e.g. db.collection.updateMany()) modifies multiple documents, the modification of each document is atomic, but the operation as a whole is not atomic.

You can read more in the documentation:MongoDB Documentation

But there are some solutions for this, try to use this library: https://www.npmjs.com/package/mongoose-transactions

1 Comment

MongoDB 4.0 does support ACID transactions for multiple documents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.