Multi-collection, multi-document 'transactions' in MongoDB

前端 未结 3 424
悲&欢浪女
悲&欢浪女 2020-12-25 14:47

I realise that MongoDB, by it\'s very nature, doesn\'t and probably never will support these kinds of transactions. However, I have found that I do need to use them in a so

3条回答
  •  梦谈多话
    2020-12-25 14:59

    MongoDB 4.0 adds support for multi-document ACID transactions.

    Java Example:

    try (ClientSession clientSession = client.startSession()) {
       clientSession.startTransaction();
       collection.insertOne(clientSession, docOne);
       collection.insertOne(clientSession, docTwo);
       clientSession.commitTransaction();
    }
    

    Note, it works for replica set. You can still have a replica set with one node and run it on local machine.

    • https://stackoverflow.com/a/51396785/4587961
    • https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/

提交回复
热议问题