Video Transcript (English)
We're preparing to launch the reviews feature for our bookstore app on our main dot-com site.
Before we do this, we want to enable schema validation for the reviews collection to make sure that only review documents with the desired shape or schema are added to the collection.
In this video we'll discuss how to use the schema validation feature in MongoDB to create and enforce rules governing the structure of documents.
In particular, we’ll cover three important concepts: validation rules, validation levels, and validation actions.
Let’s start with validation rules.
Thanks to MongoDB’s flexible schema, documents with different shapes can coexist in the same collection and their structure can be modified at any time.
But once your application is established and you know how you want to organize your data, schema validation ensures that there are no unintended schema changes, or improper data types and values.
For example, if we want usernames to be stored as a string, adding validation rules can help us make sure that a new user document won’t be written to the database if the username field value is not a string.
In general, the schema acts as a contract between database users because schema changes cannot be made unless all stakeholders agree.
MongoDB’s Schema Validation feature lets you create validation rules for your fields and gives you control over what is written to the database.
We can define these rules when creating a new collection.
MongoDB will then validate documents when they are inserted or updated in this new collection.
If you add validation rules to an existing collection, only newly inserted documents are checked for validation.
Documents that are already in the collection are only validated when they are updated.
To define schema validation rules, you can use query operators or the JSON Schema standard.
JSON schema is a well known standard that allows you to describe and validate JSON documents.
The MongoDB $jsonSchema operator is used to define validation rules for your document fields using the JSON schema standard.
Let’s look at an example.
Here, we’re creating a new collection and we define the validation rules using both a query operator and the $jsonSchema operator.
The $lt operator specifies that items.discountedPrice should be less than items.price, and $jsonSchema specifies that the items field must be an array.
Nice! But you may be asking: What happens to existing documents in the collection after we define schema validation rules?
Great question!
MongoDB offers a validation level option to control the enforcement.
There are two options: Strict, which applies rules to all inserted and updated documents, and moderate, which only applies rules to new and existing documents which have been validated.
Keep in mind invalid documents that existed before the validation was set are not checked for validity when they are updated.
So how do you identify invalid documents after you’ve established validation rules?
You can run a find query with the $nor operator to find the documents that don't match the schema.
At this point, we have defined our schema validation and chosen a level of enforcement.
Next we need to decide what happens when a document fails validation.
This is known as the validation action.
Much like the validation level, the validation action has two options: Error, which rejects insert and update operations that fail validation, and warn: where the operations complete successfully, however the validation failure is recorded in the log.
The default action is to return an error.
Let's see an example of schema validation in our Bookstore app.
We want to ensure that these fields exist in review documents and have the correct type.
To do this, first we need to define the schema for review documents.
Here, we define the jsonSchema document that will be used as the value of the $jsonSchema operator.
The schema document begins with an optional field named bsonType.
The field can be omitted but if it is set then the value must be “object.”
Next, the required array includes all the fields that must be present in the document.
And, we set additionalProperties to false to indicate only the fields under required are allowed; any other fields in the document will trigger a validation failure.
And finally, the properties document, which includes the required bsonType for each field we want to validate.
We’re also using keywords to define rules for two of our properties.
We use minimum and maximum to specify that a rating must be between 0 and 5, inclusive.
And we use maxItems to limit the size of the comments array to 3. This way we prevent an unbounded array.
Let’s put everything together now and get ready to enable validation!
Since we are modifying an existing collection, we use the collMod command to enable schema validation for the reviews collection.
We supply schemaValidation as the validator document.
And specify the desired validation level and action.
In our case we want to be strict and abort operations when validation fails to avoid unwanted documents in the database.
Great, we have now enabled schema validation for the reviews collection in the bookstore.
Now, all the documents inserted or updated in the reviews collection must satisfy the specified schema validation rules.
Let's recap what you learned in this lesson.
Schema validation allows you to enforce rules governing the structure of the documents in your application.
With MongoDB’s schema validation feature you can use the JSON Schema standard or query operators to define validation rules.
And in addition to validation rules, we can also specify validation levels and actions.
Finally, we walked through how to set up schema validation rules for the reviews collection in our bookstore application.
Great job! See you in the next lesson!
