DEV Community

Dipayan Sanyal
Dipayan Sanyal

Posted on

Write Operation / MongoDB


db.<collection>.insert([{},{}], {ordered:false})

If we set ordered to false, then inserting a set of documents (some of with same id as previously inserted docs) will not completely bring the insertion functionality to a halt. It would rather look for other documents as well hoping to find new ids to insert.

db.<collection>.insert([{},{}], {w:1, j: undefined})

w simply means to write. And the number indicates to how many instances, in case we are using multiple instances on one server, we want this write to be acknowledged. w : 1 is the default and it says hey the mongodb server should have accepted that write. The storage engine is aware of it and will eventually write it to the disk. This basically acts like an acknowledgement; after the write is successful, the server sends out an acknowledgement.
If w: 0 , then the write is superfast, but you dont receive any acknowledgement if the write was successful..

The j stands for the journal.The journal is a separate to-do file used by the storage engine to write on before actually writing it to the disk. This ensures security if the server fails writing to the disk in case of a failure. Writing to the journal has less overhead because it simply stores the query.

The above will only be done if we set j : true.

There is also the option of wtimeout, which indicates a timeout during the write operation.. Say you have a network failure in the event of a write, the wtimeout gives the option to hold on to the query till the network reconnects. This also is a security feature, that ensures a smooth write operation.

db.<collection>.insert([{},{}], {w: 1, j: true, wtimeout: 100})

Top comments (0)