I am using labix as driver and I want to make transactions over couple collections, I found link http://blog.labix.org/2012/08/22/multi-doc-transactions-for-mongodb
and I want to update in collection Owner
and Employer
not by Id
but by Name
field in collection. How can I achieve this ( simple switching Id with Name does not work ).
runner := txn.NewRunner(tcollection)
ops := []txn.Op{{
C: "accounts",
Id: "aram",//Name
Assert: M{"balance": M{"$gte": 100}},
Update: M{"$inc": M{"balance": -100}},
}, {
C: "accounts",
Id: "ben",//Name
Assert: M{"valid": true},
Update: M{"$inc": M{"balance": 100}},
}}
id := bson.NewObjectId() // Optional
err := runner.Run(ops, id, nil)
I don't believe this is possible using the Op method based on the documentation. I believe the reasoning behind disallowing this is that if you were to allow transactions based on a set field rather than by the mongo-assigned ID, there is a risk that changes can be made on the wrong document (what if someone else has the name Ben that you don't want to apply this change to?).
Since transactions like this in Mongo are usually done by 2-phase staging it makes sense to me that the limitation for this usage is that you have to make an array of transaction Ops based on IDs you find by running a separate query by the account names.