Simple way to atomically increment/decrement field?

18 views
Skip to first unread message

Judson Stephenson

unread,
Feb 7, 2011, 4:44:42 PM2/7/11
to FleetDB
I am almost certain the answer is "no", but I was wondering what the
best way would be to achieve something like this for a simple scenario
of a money transfer from one bank account to another (always a good
example from an atomic query standpoint).

Collection: "accounts" -> properties: [id, balance, user]
Collection: "transfers" -> properties: [id, amount, from_user,
to_user]

So I want to create a transfer and then decrement/increment the
account.balance by the transferred amount using an atomic query, but I
don't believe that multi-write supports anything like that, instead, I
would have to get the account.balance, subtract transfer.amount from
account.balance and then do a checked-write to make sure nothing was
edited out from under me, correct?

Maybe this could be accomplished by using multi-write combined with
checked-write?

Does a multi-write query allow for "embedded" checked-writes? and
would this be the best way to complete the aforementioned scenario?

Judson

Judson Stephenson

unread,
Feb 7, 2011, 5:20:31 PM2/7/11
to FleetDB
Actually, I believe I got that backwards.

Would using a checked-write to trigger a multi-write query be the best
avenue?

Jud

Judson Stephenson

unread,
Feb 7, 2011, 5:26:43 PM2/7/11
to FleetDB
Sorry for yet another post, but the last post won't work, at least
with a checked-write, since I would need to verify the TWO account
balances before I executed the multi-write.

Is there a way to verify TWO read queries on a single checked-write,
or maybe embed a checked-write inside another checked-write?

Jud

Judson Stephenson

unread,
Feb 9, 2011, 5:58:24 PM2/9/11
to FleetDB
I should have read the docs more closely. The checked-write can accept
multi-read and multi-write, so combining those was exactly what I
needed.
Reply all
Reply to author
Forward
0 new messages