Hi,
I am learning transaction on Meteor/Mongodb
My example:
import { Mongo, MongoInternals } from 'meteor/mongo';
import { Profiles } from './profiles';
Meteor.methods({
'publish': async function (){
const { client } = MongoInternals.defaultRemoteCollectionDriver().mongo;
const session = await client.startSession();
await session.startTransaction();
let profiles = Profiles.rawCollection();
try {
const transaction1 = await profiles.update({ userId: this.userId }, { $inc: { countPublishedArticles: 1 } });
const transaction2 = await profiles.update({ userId: this.userId }, { $inc: { countPublishedArticles: 1 } });
// I made a typo here to generate exception: "$incWrong" instead "$inc"
const transaction3 = await profiles.update({ userId: this.userId }, { $incWrong: { countPublishedArticles: 1 } });
await session.commitTransaction();
} catch (err) {
await session.abortTransaction();
throw new Meteor.Error('400', err.message);
} finally {
session.endSession();
}
}
});
So the result: Because of the typo I get exception, I Meteor throws the error, BUT transaction1 and transaction2 are executed and countPublishedArticles is encreased by 2 at the end. So session.abortTransaction() does not work.
What a reason for this?
I test on localhost with Meteor: 1.8.1 and MongoDB: 4.0.6.
I would appreciate any help!