|
The insert() function takes five options, specified in an array: fsync, j, w, wtimeout and timeout. The fsync option can be set to TRUE or FALSE; FALSE is the default value for this option.
If set to TRUE, fsync forces the data to be written to the hard disk before it indicates the insertion was a success. This option will override any setting for the option w, setting it to 0. The j option can be set to TRUE or FALSE, where FALSE is the default. If set, the j option will force the data to be written to the journal before indicating the insertion was a success.
If you are unfamiliar with journaling, think of it as a log file that keeps track of the changes made to your data, before it is finally written to disk. This ensures that, were mongod to stop unexpectedly, it will be able to recover the changes written to the journal, thereby preventing your data from entering an inconsistent state.
The w option can be used to acknowledge or unacknowledge a write operation (making this option also applicable for remove() and update() operations). If w is set to 0, the write operation will not be acknowledged; set it to 1 and the write will be acknowledged by the (primary) server.
When working with replica sets, w can also be set to n, ensuring that the primary server acknowledges the write operation when successfully replicated to n nodes. w can also be set to 'majority'—a reserved string—ensuring that the majority of the replica set will acknowledge the write, or to a specific tag, ensuring that those tagged nodes will acknowledge the write.
For this option also, the default setting is 1. The wtimeout option can be used to specify how long the server is to wait for receiving acknowledgement (in milliseconds). By default, this option is set to 10000. Lastly, the timeout option allows you to specify how long (in milliseconds) the client needs to wait for a response from the database. |
|