Learning record of mongodb Master Course (day 16)


Sixteenth days

The chapter to be learned is 19 transaction development: write operation transaction. In fact, it mainly talks about a parameter writeconcert.

What is writeconcert?

As we learned in the first chapter before, mongodb in the production environment also needs a 3-node cluster at least. Then writeconert is used to control how many nodes are successfully written to when data is written.

> db.test.insert({count: 1}, {writeConcern: {w: 3})

W parameter

  • If it is a number, such as 1, 2, 3, etc., it means how many nodes need to be written
  • Of course, if the number of nodes exceeds, the system will also report an error when writing
  • If it’s all nodes, write all
  • By default, the system is major, which means that more than half of the node data is written into, indicating that the write is successful
  • If 0, it means that as long as the master node writes successfully, it means that the data is saved successfully.
  • The success of writing here is not necessarily to write to the hard disk, but also to write to the memory.

J parameter

  • The j parameter is used to control whether the write to memory is successful or the write to hard disk is successful. It is specified here that the write to log file is successful
  • There are two values of J, true: write journal, false: write memory


  • If the write time exceeds the set time, the system will prompt the write timeout, but it does not mean that the data has not been written successfully, because the master node has already written, it is almost synchronized to the slave node.

Matters needing attention

• although more than half of writeconcert is secure, it usually only sets the priority, because it is the shortest waiting time for writing delay;
• do not set writeconcert equal to the total number of nodes, because once a node fails, all write operations will fail;
• although writeconcert will increase the delay time of write operation, it will not significantly increase the cluster pressure, so whether or not to wait, the write operation will eventually replicate to all nodes. Setting writeconcert just makes the write wait for the copy before returning;
Apply {W: “priority”} to important data, and apply {W: 1} to ordinary data to ensure optimal performance.


Need to build a 3-node test environment

Test the writeconcert parameter in the replica set

db.test.insert( {count: 1}, {writeConcern: {w: "majority"}})
db.test.insert( {count: 1}, {writeConcern: {w: 3 }})
db.test.insert( {count: 1}, {writeConcern: {w: 4 }})

Configure delay node, simulate network delay (replication delay)

conf.members[2].slaveDelay = 5
conf.members[2].priority = 0

Observe the write under the replication delay and the timeout parameter

db.test.insert( {count: 1}, {writeConcern: {w: 3}})
db.test.insert( {count: 1}, {writeConcern: {w: 3, wtimeout:3000 }})


Today, I mainly learned how to use the writeconcern parameter in write operation. The construction is set to major, which is also the default value of the system, to ensure that most nodes write data successfully.

Recommended Today

Laravel service container must know

The article was forwarded from the professional laravel developer community. Original link: https://learnku.com/laravel/t To learn how to build an application with laravel is not only to learn how to use different classes and components in the framework, but also to remember allartisanCommand or all helper functions (we have Google). Learning to code with laravel is […]