MongoDB Schema Design: A Developer’s Guide to Modeling, Validation, and Relationships

Mohammad Abbas
2 min readSep 2, 2023

--

Photo by Rubaitul Azad on Unsplash

Designing a database schema is a critical step in application development. Your schema will impact how you read from and write to the database, affecting your application’s performance and scalability. This article will guide you through modeling schemas in MongoDB, schema validation, and understanding data relationships.

Modelling Schemas Based on Application Needs

When you model your schemas in MongoDB, you should consider:

Read and Write Frequency

  • High-frequency reads: Optimize by indexing the fields that are often queried.
  • High-frequency writes: Consider sharding or horizontal partitioning.

Relations

  • Understand the relationships between different entities in your application. Will you use embedded documents or references?

Amount and Size of Data

  • Consider the volume of data that your application will handle. Large data might require sharding or splitting across multiple collections.

Schema Validation

MongoDB allows you to define validation rules that checks data before it’s written to the database.

Validation Rules

Here’s how you can define a simple validation rule for a students collection that requires a name and an age.

db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "age"],
properties: {
name: { bsonType: "string" },
age: { bsonType: "int" }
}
}
}
});

Validation Level and Action

Choose the validation level (strict or moderate) and action (error or warn) based on your application requirements.

db.runCommand({
collMod: "students",
validator: { /* new validator */ },
validationLevel: "moderate",
validationAction: "error"
});

Modelling Relations

In MongoDB, you have two main options for representing relationships between data: embedded documents and references.

Embedded Documents

  • When to Use: Opt for embedded documents if you have one-to-one or one-to-many relationships and no application or data size reason to separate the data.
{
name: "John",
age: 21,
address: {
street: "123 Main St",
city: "Anytown"
}
}

References

  • When to Use: Choose references if the data amount/size or application needs require it, or for many-to-many relations.
// Student Document
{
_id: ObjectId("507f1f77bcf86cd799439011"),
name: "John",
age: 21
}

// Address Document
{
_id: ObjectId("507f191e810c19729de860ea"),
student_id: ObjectId("507f1f77bcf86cd799439011"),
street: "123 Main St",
city: "Anytown"
}

Exceptions

  • Always remember that these are guidelines. Depending on your specific application requirements, exceptions are possible.

Summary

Effective MongoDB schema design involves a balance between your application’s read and write patterns, data size, and the relationships between entities. By combining these aspects effectively, you can create a database schema that’s optimized for your application’s specific needs.

--

--