我是 MongoDB 新手 我在银行做了一个简单的申请账户。一个账户可以给别人转账 我这样设计帐户收集
account
{
name:A
age: 24
money: 100
}
account
{
name:B
age: 22
money: 300
}
假设用户A为用户B转账100$,有2个操作: 1) 用户 A 减少 100$//文档 A 的更新 2) 为用户 B 增加 100$//更新文档 B 说atomic只申请单文档,不申请多文档。
我有一个不同的设计
Bank
{
name:
address:
Account[
{
name:A
age: 22
money: SS
},
{
name:B
age: 23
money: S1S
}
]
}
我有一些问题:
- If I use later way, How can I write transaction query (Can I use findAndModify() function?) ?
- Does MongoDB support transaction operations like Mysql (InnoDB)?
- Some pepple tell me that use Mysql for this project is the best way, and just only use MongoDB to save transaction information.(use extra collection named Transaction_money to save them), If I use both MongoDB and Mysql (InnoDB) how can make some operations below are atomic (fail or success whole):
> 1) -100$ with user A
> 2) +100$ with user B
> 3) save transaction
information like
transaction
{
sender: A
receiver: B
money : 100
date: 05/04/2013
}
非常感谢。
最佳答案
我不确定这是你要找的:
db.bank.update({name : "name"},{ "$inc" : {'Account.0.money' : -100, 'Account.1.money' : 100}})
- update() 操作满足 (ACID) 的 ACI 属性。持久性 ( D ) 取决于查询时的 mongo 和应用程序配置。
- 您可以更喜欢使用 findAndModify(),它不会在页面错误时产生锁定
- MongoDB 在文档中提供事务处理
我不明白,如果您的应用程序要求非常简单,那么您为什么要尝试使用 mongodb。毫无疑问,它是一个很好的数据存储,但我想 MySql 将满足您的所有要求。
仅供引用:有一个文档正是您要解决的问题。 http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/ 但我不建议您使用它,因为单个查询(转账)已变成查询序列。
希望对你有帮助
关于mysql - MongoDB 中的 Atomic 转账,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372506/