I thought about updates in mongo db and I would like to know if is possible just request a update to mongo but without the necessity to know if documents were updated in fact. Example: suppose a collection with 1 billion documents and I would like to update 1 document but I didn't need to wait the mongo db confirmation about this update. I could just to know if mongo db received this update request and it can be done in other moment.
Is it possible?
CodePudding user response:
It is certainly technically possible to do this, but what are you trying to achieve in doing so?
Tuning the durability guarantees for successfully acknowledging write operations is done via Write Concern. Typically, and indeed by default, write concern is set to "majority"
which guards against acknowledged data from being subsequently rolled back and lost. This can be tuned down via the w
parameter where this part of the documentation is relevant to your question:
w: 0
Requests no acknowledgment of the write operation. However,
w: 0
may return information about socket exceptions and networking errors to the application. Data can be rolled back if the primary steps down before the write operations have replicated to any of the secondaries.
Often operations that use such a write concern are referred to as "fire and forget". Sometimes this was used historically to when the application was okay with not capturing all data, but that is becoming increasingly uncommon nowadays.
Going back to the concern expressed at the beginning, it's not very clear that this behavior is actually what you want. Specifically you mention the following:
I could just to know if mongo db received this update request and it can be done in other moment.
Having the database receive the request is different than having the database durable capture the changes. Your text their implies that you are okay if the write is captured asynchronously later, but you don't actually state that you are okay if the write is lost entirely. Assuming that you want the write, I'd suggest using a write concern of at least 1
, if not "majority"
as is the default.
What specific outcome are you trying to achieve with this? You mention that the collection has 1 billion documents and that an operation will just update one of them. How is size of the collection relevant to write acknowledgement? If this is about efficiency then you'll want to be sure that you have the environment appropriately configured (specifically that you have correct indexes in place).