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.

Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94

2 Answers2

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.

Proghero
  • 659
  • 1
  • 9
  • 20
  • Thanks for telling me, But is it possible to do with only using `Callbacks`? – Sudhanshu Gaur Jul 11 '18 at 19:22
  • Yes @sudhanshu-gaur, you can pass a callback function as the last argument of `mongoose.startSession()`: http://mongoosejs.com/docs/api.html#mongoose_Mongoose-startSession – Proghero Jul 11 '18 at 21:39
-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