Wednesday 16 June 2021

MongoDB: Working with write concern

Using write concern, you can specify the level of acknowledgment requested from MongoDB for write operations.

 

Below table summarizes all the fields that are part of write concern.

 

Field

Default value

Description

w

1

Using this field, we can request acknowledgement that the write operation has propagated to a specified number of mongod instances.

 

w: 1

Request acknowledgement from primary mongod server.

 

w: 0

Request no acknowledgement from the mongod server.

 

w > 1

Request acknowledgment from the primary and as many data-bearing secondaries as needed to meet the specified write concern. For example, w : 3 request acknowledgment from the primary and 2 secondaries.

j

undefined or false

j:true specifies that the write operation should be written to journal.

wtimeout

0

Specifies the write timeout in milliseconds. wtimeout is only applicable for w values greater than 1.

 

wtimeout: 0 is equivalent to a write concern without the wtimeout option.

 

Not expecting acknowledgement from mongodb server.

> db.employee.insert({firstName : "Krishna", lastName : "Gurram", age : 31}, {writeConcern : {w: 0}})
WriteResult({ })

 

Expecting acknowledgement and write to journal

> db.employee.insert({firstName : "Siva", lastName : "Ponnam", age : 34}, {writeConcern : {j: true}})
WriteResult({ "nInserted" : 1 })



Previous                                                    Next                                                    Home

No comments:

Post a Comment