In Moon Modeler, you can easily design MongoDB database structures and generate scripts for schema validation. Many schema validation rules are accessible directly in the user interface, and additional rules can be defined textually.
Let’s see how to set the validation to the minimum and maximum value, for example. As an example, let’s use the “students” collection, in which we have defined the fields “name“, “year” and “major“.
In the “year” field we want to store records that can range from the year 2000 to the year 3000.
MongoDB schema validation in Moon Modeler
This minimum and maximum value setting can be entered in Moon Modeler in the Validation section of the “year” field details.
In the following screenshot you can see the generated script for MongoDB schema validation.
Now we can run the generated script in MongoDB.
Since the collection is empty for now, let’s try to insert a record that matches all the criteria.
As you can see, the record was saved in the database. Now let’s try to save a record that does not match the minimum required value.
The record could not be saved to the database.
How to easily find out what changes made in Moon Modeler will affect the generated script and how?
The solution is simple. Select the object in the diagram, activate the Script tab on the right side. Open the modal dialog with the object properties (e.g. by double-clicking). As soon as you make a change in the modal dialog, it will immediately appear on the right side in the preview of the generated script.
Thanks for reading!