-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Component/s: CRUD, Read and Write Concern
-
None
Typically, write concern errors are only be returned as part of the response to write commands, and in those cases the response will always be ok: 1.
There are some other cases in which a write concern error can be returned outside of such a context, such as aggregate with $merge (see SERVER-44827). In those cases, a write concern error can be included in both ok: 0 (i.e. command error) and ok: 1 responses. It is unclear how statically typed drivers (which have separate structures for WriteException and CommandException) are expected to handle this.
A few options are possible:
- When encountering ok: 1 + WC error, throw a WriteException. When encountering ok: 0 + WC error, discard the WC error and throw a CommandException.
- Amend CommandException to have an option WC error field. Still throw WriteError in the ok: 1 case.
I'm not sure if either of these options are desirable though. Another more drastic solution would be to separate $out and $merge into their own helpers and advise users against using the stages directly in their own pipelines, similar to how we handle the $changeStream stage. This would allow us to define more specific errors for these cases, but it may be too late in the game to start doing that (for $out at least).