How can you implement a check that the data is recorded in the database.
1 answer
For this purpose, MongoDB specifically introduced the clear Write Concern . Excerpt from the documentation:
write concern
Specifies whether it is done. Write concern or unavailable
mongod
instances. For replica sets , you can specify the number of members. See Write Concern .
Literally: Indicates whether the write operation was successful. "Write concern" allows your applications to detect mongod insertion errors or unavailability. For replica-set, you can specify write concern to acknowledge replication to a given number of replicas.
There is a specific set of values for this parameter:
- Unacknowledged
- Acknowledged (by default)
- Journaled
- Replica Acknowledged
By default, the driver uses the Acknowledged level - mongod
in this case will inform the client about the recording results. Those. The client may catch network errors, receive a duplicate key
message or other error. (Everything is written in the documentation).
The value of write concern in the driver can be changed in the MongoClient class .