StrictModeError not thrown on findOneAndUpdate with unknown field
0 reputation · 06 Oct 2024, 13:34 UTC
Background
Mongoose’s strict option controls how unknown fields are handled. When set to 'throw', attempting to set a field not defined in the schema should raise a StrictModeError. This behavior is well‑documented for document creation and doc.set() operations.
Unresolved Behavior
During findOneAndUpdate calls that use MongoDB update operators such as $set, the error is not consistently thrown even when strict: 'throw' is specified. The operation sometimes silently strips the unknown field, leading to data loss without an explicit error.
Goal
Determine the exact conditions under which StrictModeError is suppressed during findOneAndUpdate when strict: 'throw' is enabled, and whether this is a documented limitation or an unintended bug.
Questions
- Is the suppression of
StrictModeErroronfindOneAndUpdatewith unknown fields a documented feature or a known issue? - What role does the
runValidatorsflag play in this scenario, if any? - Are there any configuration workarounds or recommended patterns to enforce strictness on update operators?