CollectionA.insert(data, (error, _id)=>{
if (_id) {
// _id returns a value if the insert is successfully and error if not.
CollectionB.insert(data, (error, _id2) => {
if (error) {
// error here means CollectionB insert failed.
// Remove previous successfully insert _id from CollectionA
CollectionA.remove({_id: _id});
// you should probably throw an Error here
}
});
}
})
I have used this method before to implement minor transaction.