Open isayme opened 5 years ago
MongoDB 开启复制集后会记录数据的写操作, 即为 oplog.
批量操作时被更新的文档写入时会有独立的记录.
oplog 记录格式参考: MONGODB: UNDERSTANDING OPLOG
op 是 i, o 为被插入数据信息.
op
i
o
// db.tasks.insert({ title: "1" }) { "ts" : Timestamp(1547176087, 1), "t" : NumberLong(1), "h" : NumberLong("-8215838784868636031"), "v" : 2, "op" : "i", "ns" : "test.tasks", "ui" : UUID("860c1ed6-2f14-442e-9721-c02411a36864"), "wall" : ISODate("2019-01-11T03:08:07.972Z"), "o" : { "_id" : ObjectId("5c380897180e87351017cf46"), "title" : "1" } }
op 是 d, o 为被删除数据_id信息.
d
_id
// db.tasks.remove({ _id: ObjectId("5c380897180e87351017cf46") }) { "ts" : Timestamp(1547176327, 1), "t" : NumberLong(1), "h" : NumberLong("8005594350040157058"), "v" : 2, "op" : "d", "ns" : "test.tasks", "ui" : UUID("860c1ed6-2f14-442e-9721-c02411a36864"), "wall" : ISODate("2019-01-11T03:12:07.136Z"), "o" : { "_id" : ObjectId("5c380897180e87351017cf46") } }
$set
o 是 u, o 是更新后的完整数据.
u
// db.tasks.update({ _id: ObjectId("5c380897180e87351017cf46") }, { title: "2", note: "3"}) { "ts" : Timestamp(1547176647, 1), "t" : NumberLong(1), "h" : NumberLong("-1033519284697496909"), "v" : 2, "op" : "u", "ns" : "test.tasks", "ui" : UUID("860c1ed6-2f14-442e-9721-c02411a36864"), "o2" : { "_id" : ObjectId("5c380897180e87351017cf46") }, "wall" : ISODate("2019-01-11T03:17:27Z"), "o" : { "_id" : ObjectId("5c380897180e87351017cf46"), "title" : "2", "note" : "3" } }
o 是 u, o 是更新的部分.
// db.tasks.update({ _id: ObjectId("5c380897180e87351017cf46") }, { $set: { title: "4" } }) { "ts" : Timestamp(1547176767, 1), "t" : NumberLong(1), "h" : NumberLong("7121079342694515350"), "v" : 2, "op" : "u", "ns" : "test.tasks", "ui" : UUID("860c1ed6-2f14-442e-9721-c02411a36864"), "o2" : { "_id" : ObjectId("5c380897180e87351017cf46") }, "wall" : ISODate("2019-01-11T03:19:27.454Z"), "o" : { "$v" : 1, "$set" : { "title" : "4" } } }
Update Operators
$setOnInsert 记录为 $set $inc 记录为 $set $currentDate 记录为 $set $min/$max 记录为 $set $mul 记录为 $set $rename 记录为 $set + $unset $unset 记录为 $unset
$setOnInsert
$inc
$currentDate
$min/$max
$mul
$rename
$unset
$ 记录为 $set $.[] 记录为 $set $.[element] 记录为 $set, 格式有两种: $set: { field: [v1, v2] } 和 $set: { 'field.x': v } $addToSet 记录为 $set $pop 记录为 $set $pull 记录为 $set $pullAll 记录为 $set $push 记录为 $set, $set: { 'field.x': v }
$
$.[]
$.[element]
$set: { field: [v1, v2] }
$set: { 'field.x': v }
$addToSet
$pop
$pull
$pullAll
$push
MongoDB 开启复制集后会记录数据的写操作, 即为 oplog.
批量操作时被更新的文档写入时会有独立的记录.
oplog 记录格式参考: MONGODB: UNDERSTANDING OPLOG
插入
op
是i
,o
为被插入数据信息.删除
op
是d
,o
为被删除数据_id
信息.更新
不使用
$set
o
是u
,o
是更新后的完整数据.使用
$set
o
是u
,o
是更新的部分.其他更新操作
Update Operators
$setOnInsert
记录为$set
$inc
记录为$set
$currentDate
记录为$set
$min/$max
记录为$set
$mul
记录为$set
$rename
记录为$set
+$unset
$unset
记录为$unset
$
记录为$set
$.[]
记录为$set
$.[element]
记录为$set
, 格式有两种:$set: { field: [v1, v2] }
和$set: { 'field.x': v }
$addToSet
记录为$set
$pop
记录为$set
$pull
记录为$set
$pullAll
记录为$set
$push
记录为$set
,$set: { 'field.x': v }