Flexibility in Handling Wrong Data Types in Models


this might be a long shot, but…

I have written a data model, where the fields in the schema all have their defined data types (String, Date, etc), and then I read some data from a database and attempt to write that data to MongoDB using my data model. However, several of the data entries have the wrong type in one of the fields – e.g., instead of a Date, there is a JS Object. Trying to create a document from this data using my model is going to throw a ValidationError because that field contains an Object rather than a Date for those entries.

Is there any way to get around throwing a ValidationError and allow for some flexibility in the data type of fields? I.e., is it possible to code the schema or model in such a way that, in my example, instead of throwing a ValidationError, the compiler heaves a sigh and creates a document with an Object in that field instead of a Date only for those particular data entries?

Hello @Francesca_Ricci-Tam, you can try using the procedure described in the following community post: Validate schema with conditions

Also refer the MongoDB Manual about Schema Validation and $jsonSchema - keywords