- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
MongoDB 4.0 Features - Transactions and More
MongoDB 4.0增加了对多文档ACID事务的支持,将文档模型与ACID保证结合起来。通过快照隔离,事务提供一致的数据视图,并强制执行全部或全部执行,以维护数据完整性。
虽然本次网络研讨会主要关注MongoDB事务(主要的MongoDB 4.0功能)和未来的任何事务改进,但我们还将介绍其他MongoDB 4.0功能,如无阻塞二次读取、安全改进等。
在参加网络研讨会之后,您将了解更多关于令人兴奋的新MongoDB4.0特性的信息。
展开查看详情
1 .
2 .My name is Alexander Rubin • Working with MySQL for over 12 years – Started at MySQL AB, then Sun Microsystems, – then Oracle (MySQL Consulting) – Joined Percona 5 years ago • Working with MongoDB for 3 years
3 .▪ • ▪ ▪ • • •
4 .
5 .▪ •… •…
6 . ▪ ▪ ▪ ▪ ▪ • • https://support.pokemongo.nianticlabs.com/hc/en-us/articles/360001518407-Trading-Pokémon
7 . ▪ ▪ • • • • https://support.pokemongo.nianticlabs.com/hc/en-us/articles/360001518407-Trading-Pokémon
8 .// this is basic design of what Pokemon exchange can look like session = db.getMongo().startSession() session.startTransaction() session.getDatabase("p").player_items.update( {player_id : 123}, { $inc: { stardust: -1 } } ); session.getDatabase("p").player_items.update( {player_id : 321}, { $inc: { stardust: -1 } } ); ... session.getDatabase("p").player_pokemons.update( {player_id : 321}, $push: {pokemon_id: 987}); … session.commitTransaction() https://support.pokemongo.nianticlabs.com/hc/en-us/articles/360001518407-Trading-Pokémon
9 .session = db.getMongo().startSession() ● ● ○ ●
10 .● ● ● ● ● ○ ● ○
11 .● ● ●
12 .● ○ ○
13 .● ○ … ○ …
14 .● ● ●
15 .● ○ … ●
16 .● ○ ● ○ ○ ● ○ ○
17 .From MongoDB World 2018: Keynote, https://www.slideshare.net/mongodb/mongodb-world-2018-keynote
18 .● ● ○ ○
19 .● foo:PRIMARY> session = db.getMongo().startSession() session { "id" : UUID("bdd82af7-ab9d-4cd3-9238-f08ee928f31e") } foo:PRIMARY> session.startTransaction() foo:PRIMARY> session.getDatabase("percona").test.insert({today : new Date()}) WriteResult({ "nInserted" : 1 }) ● foo:PRIMARY> session = db.getMongo().startSession() session { "id" : UUID("eb628bfd-425e-450c-a51b-733435474eaa") } foo:PRIMARY> session.startTransaction() foo:PRIMARY> session.getDatabase("percona").test.find() // nothing
20 .● foo:PRIMARY> session.commitTransaction() ● foo:PRIMARY> session.getDatabase("percona").test.find() foo:PRIMARY> // nothing ● foo:PRIMARY> db.test.find() { "_id" : ObjectId("5b21361252bbe6e5b9a70a4e"), "today" : ISODate("2018-06-13T15:19:46.645Z") } { "_id" : ObjectId("5b21361252bbe6e5b9a70a4f"), "some_value" : "abc" }
21 .● use percona db.test.insert({trx : 0}) ● Then we create session1 and update trx to change from 0 to 1: foo:PRIMARY> session = db.getMongo().startSession() session { "id" : UUID("0b7b8ce0-919a-401a-af01-69fe90876301") } foo:PRIMARY> session.startTransaction() foo:PRIMARY> session.getDatabase("percona").test.update({trx : 0}, {trx: 1}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
22 .● foo:PRIMARY> session = db.getMongo().startSession() session { "id" : UUID("b312c662-247c-47c5-b0c9-23d77f4e9f6d") } foo:PRIMARY> session.startTransaction() foo:PRIMARY> session.getDatabase("percona").test.update({trx : 0}, {trx: 2}) WriteCommandError({ "errorLabels" : [ "TransientTransactionError" ], MongoDB catches the "operationTime" : Timestamp(1529675754, 1), conflict and return the "ok" : 0, error on the insert "errmsg" : "WriteConflict", (even before the commit) "code" : 112, "codeName" : "WriteConflict", "$clusterTime" : { "clusterTime" : Timestamp(1529675754, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } })
23 .● Session.startTransaction({ writeConcern: { w: <level>} }) // w: 1, majority
24 .● ● Session.startTransaction({ readConcern: { level: <level>} })
25 .● ● Session.startTransaction({ readConcern: { level: "snapshot"} })
26 .
27 .● ○ ●
28 .● ●
29 .● ○ ○