I’m relatively new to MongoDB and playing around with implementing transactions inspired by this technique:
http://cookbook.mongodb.org/patterns/perform-two-phase-commits/
I am thinking of how to block simultaneous transactions for given source/destination accounts. For example I can block simultaneous transactions on same source by declaring source a unique index:
transactionsCollection.ensureIndex({"source":1}, {unique: true});
var newDoc = {source: sourceID, destination: destinationID,
amount: 100, state:"pending"}
transactionsCollection.save(newDoc, {safe:true}, function(error, t) {
if (error.name == "MongoError" && error.code == 11001)
// duplicate index, so I'm locked out
}
But what I really want is to be locked out if either source or destination is already in the transaction table (either as source or as destination).
So my question is whether it’s possible to setup the indexes in a way that will allow the above, or if not what other ways are there to accomplish this.
Thanks!
Using a unique index on an array (
tagsbelow) inside the records should get the trick done. Example: