I want to create a send money system , When user A send $X to user B I want :
A_BALANCE -= X
)B_BALANCE += X
)I want to model the data as follow
account
collection has fields
_id
name
balance
transaction
collection has fields
_id
from_account_id
to_account_id
status
value
time
If I choose shard key of transaction
one of from_account_id
or to_account_id
I can query incoming and outgoing transaction of a specific account with Targeted Operations
But if I want to get list of transaction for specific account sorting by _id
I have Broadcast Operations
How can I model better data , If I design transaction collection as follow
transaction
collection has fields
_id
account_id
from_account_id
to_account_id
status
value
time
and choose account_id
as my shard key , and when we have a transaction I insert two same documents in transaction
collection and only change account_id
in two document , in first document account_id
is payer and in other is payee
This data model has a consistency issue , How can I sync these two records?
Is there better data modeling?