Permalink: 2013-09-22 09:09:47 by ning in mongo tags: all mongo

http://www.kchodorow.com/blog/2010/10/12/replication-internals/ 对几种操作的oplog 做了个介绍,不过有个问题, 遗漏了一种update格式.

基本格式

insert

PRIMARY> use test_mig
switched to db test_mig
PRIMARY> db.test_oplog.insert({'a': 1})
PRIMARY> db.test_oplog.insert({'a': 1})
PRIMARY> db.test_oplog.insert({'a': 2})

PRIMARY> db.getSisterDB('local').oplog.rs.find().sort({$natural: -1}).limit(5)
{ "ts" : { "t" : 1379811509000, "i" : 1 }, "h" : NumberLong("-541764781068148886"), "v" : 2, "op" : "i", "ns" : "test_mig.test_oplog", "o" : { "_id" : ObjectId("523ea865b04265100d6f0598"), "a" : 2 } }
{ "ts" : { "t" : 1379811508000, "i" : 1 }, "h" : NumberLong("-5437861844169095741"), "v" : 2, "op" : "i", "ns" : "test_mig.test_oplog", "o" : { "_id" : ObjectId("523ea864b04265100d6f0597"), "a" : 1 } }
{ "ts" : { "t" : 1379811505000, "i" : 1 }, "h" : NumberLong("7009628162923571334"), "v" : 2, "op" : "i", "ns" : "test_mig.test_oplog", "o" : { "_id" : ObjectId("523ea861b04265100d6f0596"), "a" : 1 } }

update

PRIMARY> db.test_oplog.update({'a': 2}, {'a': 2, 'b': 2})
PRIMARY> db.test_oplog.update({'a': 2}, {'a': 2222})

PRIMARY> db.getSisterDB('local').oplog.rs.find().sort({$natural: -1}).limit(5)
{ "ts" : { "t" : 1379812156000, "i" : 1 }, "h" : NumberLong("1839954846191959534"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea865b04265100d6f0598") }, "o" : { "a" : 2222 } }
{ "ts" : { "t" : 1379812099000, "i" : 1 }, "h" : NumberLong("1040690260917471023"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea865b04265100d6f0598") }, "o" : { "a" : 2, "b" : 2 } }

update by $set

PRIMARY> db.test_oplog.update({'a': 1}, {'$set': {'b': 1}})
PRIMARY> db.getSisterDB('local').oplog.rs.find().sort({$natural: -1}).limit(5)
{ "ts" : { "t" : 1379812316000, "i" : 1 }, "h" : NumberLong("-7531977215037588101"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea861b04265100d6f0596") }, "o" : { "$set" : { "b" : 1 } } }

使用multi

2.2 以前的mongoshell 是update(query, set, upsert, multi):

set_test_1:PRIMARY> db.test_oplog.update({'a': 1}, {'$set': {'b': 'yyyy'}}, {multi:true} )
set_test_1:PRIMARY> db.getSisterDB('local').oplog.rs.find().sort({$natural: -1}).limit(5)
{ "ts" : Timestamp(1379812808000, 2), "h" : NumberLong("7954591165252070733"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea864b04265100d6f0597") }, "o" : { "$set" : { "b" : "yyyy" } } }
{ "ts" : Timestamp(1379812808000, 1), "h" : NumberLong("-1963375078122908247"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea861b04265100d6f0596") }, "o" : { "$set" : { "b" : "yyyy" } } }

使用$inc

会转化为$set:

set_test_1:PRIMARY> db.test_oplog.update({'a': 1}, {'$inc': {'c': 1}}, {multi:true} )
set_test_1:PRIMARY> db.getSisterDB('local').oplog.rs.find().sort({$natural: -1}).limit(5)
{ "ts" : Timestamp(1379812901000, 2), "h" : NumberLong("3836072815439856239"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea864b04265100d6f0597") }, "o" : { "$set" : { "c" : 2 } } }
{ "ts" : Timestamp(1379812901000, 1), "h" : NumberLong("-3961481879606589185"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea861b04265100d6f0596") }, "o" : { "$set" : { "c" : 2 } } }
{ "ts" : Timestamp(1379812886000, 2), "h" : NumberLong("-8702988464634388902"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea864b04265100d6f0597") }, "o" : { "$set" : { "c" : 1 } } }
{ "ts" : Timestamp(1379812886000, 1), "h" : NumberLong("-3933979447585942296"), "v" : 2, "op" : "u", "ns" : "test_mig.test_oplog", "o2" : { "_id" : ObjectId("523ea861b04265100d6f0596") }, "o" : { "$set" : { "c" : 1 } } }

delete

set_test_1:PRIMARY> db.test_oplog.remove({'a': 1})
set_test_1:PRIMARY> db.getSisterDB('local').oplog.rs.find().sort({$natural: -1}).limit(5)
{ "ts" : Timestamp(1379813007000, 2), "h" : NumberLong("-6184108545915471156"), "v" : 2, "op" : "d", "ns" : "test_mig.test_oplog", "b" : true, "o" : { "_id" : ObjectId("523ea864b04265100d6f0597") } }
{ "ts" : Timestamp(1379813007000, 1), "h" : NumberLong("-7842531010992739410"), "v" : 2, "op" : "d", "ns" : "test_mig.test_oplog", "b" : true, "o" : { "_id" : ObjectId("523ea861b04265100d6f0596") } }

字段含义

"ts" : Timestamp(1379812901000, 2), "h" : NumberLong("3836072815439856239"),
  • ts: the time this operation occurred.
    • 8字节的时间戳,由4字节unix timestamp + 4字节秒内自增计数表示:
    • ts字段用处很多
  • h: a unique ID for this operation. Each operation will have a different value in this field.
    • h字段对回滚有影响.

注意

  1. update 有两种格式.
  2. oplog记录的是记过, 可以重放.

Comments