MongoDB: Schema Validation
You can validate schema for MongoDB forward engineering scripts. The schema validator verifies fields in collections based on their data type. If enabled, this feature restricts forward engineering script generation in case the data in the fields is not in line with the assigned data type. Thus, enabling you to avoid any errors in the forward engineering script. This feature is enabled by default for fields in collections.
In case of any exceptions, you can select the Exclude Schema Validator check box to exclude validations for one or more fields. Doing so overrides the assigned data type of a field and generates the script successfully.
This topic walks you through the steps to generate schema validation for fields in a simple table (collection) and generate schema using an example. The following table lists the fields and assigned data types in a MongoDB collection, Customer.
Fields |
Data Type |
Name | STRING |
Year | INTEGER |
Subjects | STRING |
Address | OBJECT |
Street | STRING |
City | STRING |
The following image displays the fields in a collection and the assigned data types.
To generate schema validation for a MongoDB collection, follow these steps:
- In erwin DM, create a collection with fields, and assign relevant datatypes.
Ensure that a database in assigned to the collection.
- Right-click the collection and click Collection Properties.
The MongoDB Collection Editor appears. - Go to the Schema Validator tab.
- Click to generate schema validation template.
This displays the generated schema validation template based on the field properties and assigned datatypes. - Click to edit the schema.
The Collection Schema Validator editor appears. This displays the generated schema validation template with validations in edit mode. - Edit the schema template for validations and add a description for the fields based on your requirement.
In this example, the validation properties of the fields and descriptions are updated as shown in the following image.
-
Once you have made changes to the schema template, click OK.
The schema template validation changes are saved. -
On the application ribbon, click Action > Schema.
The Forward Engineer Schema Generation Wizard appears. -
Go to Preview tab.
The updated schema with the validations appear. - In the Preview section, edit the schema to correct the random integer values.
Generating schema validation template adds random integer values to the fields with INTEGER datatype. Ensure that you replace this with valid value to generate the schema successfully.
- Click Generate.
The MongoDB Connection screen appears. Connect to the database. For more information, refer to the Database Connection Parameters topic.
The schema is generated successfully along with the validations.
The schema generation wizard displays errors if the field-level validations are not met. For example, the schema in the following image accepts values between 2020 and 2030 in the Year field. The, Year field is updated with the value 2011.
When you click Generate, the schema generation fails as the validation rules are not met and displays the following error.
Alternatively, you can exclude validations for one or more fields to generate schema. For example, you can exclude schema validation for the field, Subject (STRING), in this collection. Then, replace a string with a number (INTEGER). This overrides the assigned data type of a field and generates the script successfully.
To exclude schema validation for a field, open Collection Field Editor, select a field, and then, select the Exclude Schema Validator check box.
In the Forward Engineering Schema Wizard, when you click Generate, the schema is generated successfully.
Copyright © 2023 Quest Software, Inc. |