简体   繁体   English

交易mongodb

[英]Transaction mongodb

I need to write into two different mongodb collections using an 'all or nothing' process. 我需要使用“全有或全无”过程写入两个不同的mongodb集合。 Fyi I use NodeJs in my backend side. 方便,我在后端使用NodeJ。

As far as I know MongoDb provides atomicity when it comes to a single collection, but it does not when we need to write into multiple collections. 据我所知,MongoDb在单个集合中提供原子性,但在我们需要写入多个集合时则不提供原子性。

So I'd like to know a way of emulating this a transaction in nodejs/mongodb in order to avoid writing into one collection if the other failed and also getting the possibility of doing a 'roll back' if the second process fails. 因此,我想知道一种在nodejs / mongodb中模拟此事务的方法,以避免在另一个失败时写入一个集合,并且在第二个过程失败时也有可能进行“回滚”。

Thank you guys! 感谢大伙们!

Starting from version 4.0 MongoDB will add support for multi-document transactions. 从版本4.0开始,MongoDB将添加对多文档事务的支持。 Transactions in MongoDB will be like transactions in relational databases. MongoDB中的事务将类似于关系数据库中的事务。 For details visit this link: https://www.mongodb.com/blog/post/multi-document-transactions-in-mongodb?jmp=community 有关详细信息,请访问以下链接: https : //www.mongodb.com/blog/post/multi-document-transactions-in-mongodb?jmp=community

I wrote a library that implements the two phase commit system mentioned above. 我写了一个实现上述两阶段提交系统的库。 It might help in this scenario. 在这种情况下可能会有所帮助。 Fawn - Transactions for MongoDB Fawn-MongoDB的事务

The transactions for multi-document have been introduced in MongoDB 4.0 !!! 多文档事务已在MongoDB 4.0引入!!!

https://docs.mongodb.com/manual/core/transactions https://docs.mongodb.com/manual/core/transactions

In MongoDB (prior to 4.0) there is no way you can fully implement transactions on database level. 在MongoDB(4.0之前的版本)中,您无法在数据库级别完全实现事务。 However, there are some mechanisms which provides some transactions functionality. 但是,有些机制可以提供某些交易功能。 You can read about them in documentation . 您可以在文档中阅读有关它们的信息

Since MongoDB 4.0, transactions are supported. 从MongoDB 4.0开始,支持事务。 Very little chage is needed in your current code to support them. 当前的代码需要很少的改动来支持它们。 There's a new section in the documentation fully dedicated to the subject 文档中有专门针对该主题的新部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM