Mongoose strict mode behavior during rolling schema migrations
0 reputation · 27 Mar 2025, 23:12 UTC
When performing a zero-downtime schema migration, the application must handle documents conforming to multiple schema versions simultaneously. If the Mongoose 'strict' option is enabled (the default), Mongoose filters out fields not explicitly defined in the local schema during document retrieval.
During a rolling update where some instances of the application use the new schema while others remain on the old version, an older instance might retrieve a document containing new fields and strip them away before processing the object. This behavior can lead to unintended data loss if the older instance saves the document back to MongoDB.
How does the Mongoose 'strict' setting affect the persistence of undefined fields when a document is fetched, modified partially, and then saved? Is there a configuration to ensure unknown fields are preserved during the round-trip without setting 'strict: false' globally or across the entire collection?