Validate MongoDB Data

MongoDBBeginner
Practice Now

Introduction

In this lab, you will learn the fundamentals of data validation in MongoDB. Data validation is a crucial feature that helps maintain the integrity and consistency of your data by ensuring that all documents in a collection adhere to a specific structure and rules. You will learn how to create collections with validation rules, test these rules by attempting to insert both valid and invalid data, and modify validation rules on an existing collection. By the end of this lab, you will be able to enforce data quality directly within your MongoDB database.

Create a Collection with Schema Validation

In this first step, you will connect to the MongoDB server and create a new collection with specific validation rules. These rules will define the expected data types and constraints for the fields in your documents.

First, open the MongoDB Shell (mongosh) to interact with your database. This interactive shell is your primary tool for managing MongoDB.

mongosh

Once inside the shell, you will see a prompt like test>. This indicates you are connected to the default test database. Let's create and switch to a new database named dataValidationLab.

use dataValidationLab

Now, let's create a users collection. When creating the collection, we will include a validator document that uses $jsonSchema to define our rules.

db.createCollection("users", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["name", "age", "email"],
      properties: {
        name: {
          bsonType: "string",
          description: "must be a string and is required"
        },
        age: {
          bsonType: "int",
          minimum: 18,
          description: "must be an integer >= 18 and is required"
        },
        email: {
          bsonType: "string",
          pattern: "^.+@.+$",
          description: "must be a valid email address and is required"
        }
      }
    }
  }
});

After running the command, you should see an output indicating success:

{ "ok": 1 }

Let's break down the validator:

  • bsonType: "object": The root of the document must be an object.
  • required: ["name", "age", "email"]: The name, age, and email fields must be present in every document.
  • properties: This object defines the rules for individual fields.
    • name: Must be a string.
    • age: Must be an int (integer) with a minimum value of 18.
    • email: Must be a string that matches the regular expression pattern, which checks for a basic email format.

You have now successfully created a collection with data validation rules. In the next step, you will test these rules.

Test Validation by Inserting Documents

Now that your users collection has validation rules, let's test them. You will attempt to insert several documents: some that violate the rules and one that conforms to them. This will help you understand how MongoDB enforces the schema.

First, let's try to insert a document with an incorrect data type for the age field. We will provide a string "25" instead of an integer.

db.users.insertOne({
  name: "John Doe",
  age: "25",
  email: "john.doe@example.com"
});

This operation will fail. MongoDB will reject the document and return a WriteError because the age field does not meet the bsonType: "int" requirement. The error message will contain details about the validation failure.

MongoServerError: Document failed validation

Next, let's try to insert a document that is missing a required field, email.

db.users.insertOne({
  name: "Jane Doe",
  age: 30
});

This will also fail because the email field is listed in the required array in our validator. You will receive a similar WriteCommandError.

Finally, let's insert a document that follows all the rules. The name and email are strings, and age is an integer greater than or equal to 18.

db.users.insertOne({
  name: "Alice",
  age: 28,
  email: "alice@example.com"
});

This time, the command will succeed, and you will see a confirmation message with the _id of the newly inserted document.

{
  "acknowledged": true,
  "insertedId": ObjectId("...")
}

To confirm that the valid document was added, you can query the collection.

db.users.find();

The output will show the one document that successfully passed validation.

[
  {
    "_id": ObjectId("..."),
    "name": "Alice",
    "age": 28,
    "email": "alice@example.com"
  }
]

Modify an Existing Validator

Your application requirements might change over time, and you may need to update your validation rules. In this step, you will learn how to modify the validator of an existing collection using the collMod command and then test the new rules.

Let's say we want to add a new status field to our users collection. This field should be a string and can only have one of two values: "active" or "inactive".

We use the collMod (collection modify) command to apply the new validator.

db.runCommand({
  collMod: "users",
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["name", "age", "email", "status"],
      properties: {
        name: {
          bsonType: "string",
          description: "must be a string and is required"
        },
        age: {
          bsonType: "int",
          minimum: 18,
          description: "must be an integer >= 18 and is required"
        },
        email: {
          bsonType: "string",
          pattern: "^.+@.+$",
          description: "must be a valid email address and is required"
        },
        status: {
          enum: ["active", "inactive"],
          description: "can only be one of the enum values and is required"
        }
      }
    }
  }
});

The command will return { "ok": 1 } on success. We have now updated the validator to require the status field and restrict its value using the enum keyword.

Now, let's test the new rule. Try to insert a document with an invalid status.

db.users.insertOne({
  name: "Bob",
  age: 45,
  email: "bob@example.com",
  status: "pending"
});

This will fail because "pending" is not in the enum list for the status field. Next, insert a document that complies with the updated rules.

db.users.insertOne({
  name: "Bob",
  age: 45,
  email: "bob@example.com",
  status: "active"
});

This insertion will succeed. To see all the documents in your collection, run find() again.

db.users.find();

You will now see both Alice's and Bob's documents. Note that Alice's document, inserted before the rule change, does not have the status field. By default, validation does not apply to existing documents until they are modified.

Summary

In this lab, you have learned the essential techniques for implementing data validation in MongoDB. You started by creating a collection with a $jsonSchema validator to enforce data types, required fields, and value constraints. You then tested these rules by attempting to insert both invalid and valid documents, observing how MongoDB rejects data that does not conform to the schema. Finally, you learned how to modify the validation rules for an existing collection using the collMod command to adapt to new requirements. These skills are fundamental for building robust applications and maintaining high data quality in your MongoDB databases.