C100DBA MongoDB讲解-update in replication
题目正文:
Support you execute the following query on the primary of a replica set and that this results in updates to 130 documents in the products collection :
db.products.update( { "year": "2012" } , { "discount" : 0.1} , { multi: true } )
which of the following best describes replication in this case?
- Each Secondary executes the same query
- Each Secondary executes one operation per document affected
- Each Secondary writes a byte-for-byte of data file changes on the primary to its own data files.
- It depends on the configuration of the replica set
- None of the above describes replication correctly
翻译:
假设你在一个replica set复制集的primary上执行了下面的语句,并更新了130个products collection中的document文档,那么:
db.products.update( { “year”: “2012” } , { “discount” : 0.1} , { multi: true } )
那么如下哪个选项关于在此场景中的replication描述是正确的?
- 所有secondary将执行同样的语句
- 所有secondary将针对受影响的document一个一个做更新操作(其实oracle的Streams、OGG也是这样)
- 所有的secondary都对primary上数据文件的修改一个个字节地做对应的修改 (其实相当于Oracle 物理备库 physical standby)
- 取决于实际replica set的配置
- 以上说的关于replication 都不对啊
官方文档对于secondary应用oplog的解释是:
Updates to Multiple Documents at Once
The oplog must translate multi-updates into individual operations in order to maintain idempotency. This can use a great deal of oplog space without a corresponding increase in data size or disk use. http://docs.mongodb.org/manual/core/replica-set-oplog/
即oplog必须将多更新的操作转换成单独的操作 以便维护幂等性idempotency,idempotency的含义是一个操作保证了这个操作无论被执行多少次都降产生同样的结果。
显然答案B是正确的,具体验证需要通过TRACE Secondary 来证明。
但题目中给出的update语句是存在问题的:
> db.products.insert({ year:"2012"}); WriteResult({ "nInserted" : 1 }) > db.products.update( { "year": "2012" } , { "discount" : 0.1} , { multi: true } ) WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 9, "errmsg" : "multi update only works with $ operators" } }) >
Leave a Reply