node.js 如何在mongo中实现事务回滚?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21487233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to achieve rollback in transactions in mongo?
提问by codeofnode
I am using mongoose over mongodb.
我在 mongodb 上使用猫鼬。
In a request and response from nodejs express server, generally requires a no of queries to mongodb through mongoose.
在来自 nodejs express 服务器的请求和响应中,通常不需要通过 mongoose 对 mongodb 进行查询。
In many cases it may happen, if first a few queries executes successfully and rest all failed under a transaction.
在许多情况下,如果前几个查询成功执行而其余所有查询都失败,则可能会发生这种情况。
Now i need to rollback my db state to the very initial state of db.
现在我需要将我的 db 状态回滚到 db 的初始状态。
So how can i rollback efficiently in mongodb? Please provide some suitable references.
那么如何在 mongodb 中高效回滚呢?请提供一些合适的参考资料。
采纳答案by Dharshan
I agree with Hector. If you need multiple document transactions then Mongo might not be a good fit for you. Can you embed your data structure in a single document? Then you can get the rollback you are looking for.
我同意赫克托。如果您需要多个文档事务,那么 Mongo 可能不适合您。您可以将数据结构嵌入到单个文档中吗?然后你可以得到你正在寻找的回滚。
Another option is to look at the Mongo clone TokuMX which provides transactions across multiple documents.
另一种选择是查看 Mongo 克隆 TokuMX,它提供跨多个文档的事务。
回答by codeofnode
Going through mongo tutorials
学习 mongo 教程
Here is a link i found for making two phase commits ( may be it is not guaranteed ) :
这是我找到的用于进行两阶段提交的链接(可能无法保证):
http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/
http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/
回答by Hector Correa
There is no support for traditional ACID database transactions in MongoDB regardless of what you put on top of it. This is not an issue with Mongoose or Express.js, it's a characteristic of MongoDB.
MongoDB 中不支持传统的 ACID 数据库事务,无论您在其上放置什么。这不是 Mongoose 或 Express.js 的问题,而是 MongoDB 的一个特征。
MongoDB supports atomic operations which means that saving to a collection is guaranteed to succeed completely or fail, in other words you won't get partial updates to a single document in a collection. But there is no support for multi-updates in a single transaction.
MongoDB 支持原子操作,这意味着保存到集合可以保证完全成功或失败,换句话说,您不会对集合中的单个文档进行部分更新。但是不支持在单个事务中进行多次更新。
http://docs.mongodb.org/manual/faq/fundamentals/#does-mongodb-support-transactions
http://docs.mongodb.org/manual/faq/fundamentals/#does-mongodb-support-transactions

