说明
分布式系统,使用 MongoDB 的 OPlog 重放机制来实现数据同步。
版本
MongoDB:3.0.3
格式记录
插入 document
use store db.book.insert({"name":"think diffenert"})
{ "ts" : Timestamp(1480571578, 1), "op" : "c", "ns" : "store.$cmd", "o" : { "create" : "book" } } { "ts" : Timestamp(1480571578, 2), "op" : "i", "ns" : "store.book", "o" : { "_id" : ObjectId("583fbaba3d08d395847cb7b2"), "name" : "think diffenert" } }
|
修改 document
db.book.insert({"name":"hello world"}) db.book.update({"name":"hello world"},{$set:{'name':'你好 世界'}})
{ "ts" : Timestamp(1480571707, 2), "op" : "i", "ns" : "store.book", "o" : { "_id" : ObjectId("583fbb3b3d08d395847cb7b3"), "name" : "hello world" } } { "ts" : Timestamp(1480571912, 1), "op" : "u", "ns" : "store.book", "o2" : { "_id" : ObjectId("583fbb3b3d08d395847cb7b3") }, "o" : { "$set" : { "name" : "你好 世界" } } }
|
删除 document
db.book.insert({"name":"big world"}) db.book.remove({"name":"big world"})
{ "ts" : Timestamp(1480572003, 1), "op" : "i", "ns" : "store.book", "o" : { "_id" : ObjectId("583fbc633d08d395847cb7b4"), "name" : "big world" } } { "ts" : Timestamp(1480572010, 1), "op" : "d", "ns" : "store.book", "b" : true, "o" : { "_id" : ObjectId("583fbc633d08d395847cb7b4") } }
|
重命名 collections 字段名
db.book.update({}, {$rename : {"name" : "bookname"}}, false, true)
{ "ts" : Timestamp(1480572184, 1), "op" : "u", "ns" : "store.book", "o2" : { "_id" : ObjectId("583fbaba3d08d395847cb7b2") }, "o" : { "$set" : { "bookname" : "think diffenert" }, "$unset" : { "name" : true } } } { "ts" : Timestamp(1480572184, 2), "op" : "u", "ns" : "store.book", "o2" : { "_id" : ObjectId("583fbb3b3d08d395847cb7b3") }, "o" : { "$set" : { "bookname" : "你好 世界" }, "$unset" : { "name" : true } } }
|
新建索引
db.book.ensureIndex({bookname:1})
{ "ts" : Timestamp(1480572622, 1), "op" : "i", "ns" : "store.system.indexes", "o" : { "ns" : "store.book", "key" : { "bookname" : 1 }, "name" : "bookname_1" } }
|
重命名 collections
db.book.renameCollection( "book2016" )
{ "ts" : Timestamp(1480572878, 1), "op" : "c", "ns" : "admin.$cmd", "o" : { "renameCollection" : "store.book", "to" : "store.book2016", "dropTarget" : null } }
|
删除索引
db.book2016.dropIndex({"bookname":1})
{ "ts" : Timestamp(1480573326, 1), "op" : "c", "ns" : "store.$cmd", "o" : { "deleteIndexes" : "book2016", "index" : { "bookname" : 1 } } }
|
删除 collections
db.book2016.drop; { "ts" : Timestamp, "op" : "c", "ns" : "store.$cmd", "o" : { "drop" : "book2016" } }
|
重命名 db
db.adminCommand({renameCollection: "store.book", to: "store1.book"}) { "ts" : Timestamp(1480573827, 1), "op" : "c", "ns" : "admin.$cmd", "o" : { "renameCollection" : "store.book", "to" : "store1.book" } }
|
删除 db
use store db.dropDatabase()
{ "ts" : Timestamp(1480571443, 1), "op" : "c", "ns" : "store.$cmd", "o" : { "dropDatabase" : 1 } }
|
执行OPlog
{ "ts" : Timestamp(1481844533, 5), "op" : "c", "ns" : "vbox_server.$cmd", "o" : { "applyOps" : [ { "h" : NumberLong("-4287702401767760790"), "ts" : Timestamp(1481844533, 7), "o" : { "$set" : { "lastmodify" : NumberLong("1481844533728") } }, "v" : 2, "ns" : "vbox_server.vbox_client_profile", "o2" : { "_id" : ObjectId("568b951c9892d6406aab673a") }, "op" : "u" } ] } }
|