What is the best way to define schema validation rules?


I received a lot of JSON files to store in a new collection and create schema validation rules. Is there a way or tool to read JSON files and suggest schema validation rules?

The best way that I found was to insert all the files in a collection, run the Analyze Schema in compass, and create the rules.

1 Like

Hello @Rafael_Martins ,

I notice you haven’t had a response to this topic yet - were you able to find a solution?
If not, can you please explain more about the purpose of your schema validation and what are you trying to achieve because inserting documents in the collection and then creating rules seems like a reverse workflow. The purpose of schema validation is to enforce that documents in a collection conform to a certain criteria, and the schema was typically created during the design phase of the app, instead of being created after the fact. Schema validation lets you create validation rules for your fields, such as allowed data types and value ranges.

MongoDB uses a flexible schema model, which means that documents in a collection do not need to have the same fields or data types by default. Once you’ve established an application schema, you can use schema validation to ensure there are no unintended schema changes or improper data types.

If you just want to use the same schema analysis outside of Compass, you can try this mongodb-schema command line tool, it is open source (Apache 2.0 license) and usable as a Node.js library.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.