MongoDB Foundations·Documents with discipline

Indexes, validation and growth

Indexes support your real queries; validation protects the document shape.

Beginner 15 min read Lesson 10 of 300 Challenge included
What you will be able to do
  • Use explain("executionStats") to confirm an index is used.
  • Order compound index fields by equality, sort, range.
  • Use JSON Schema validation for critical collections.

In plain English

A MongoDB index is a B-tree-like structure ordered by fields. Compound indexes follow the ESR rule: equality, sort, range. Schema validation catches malformed writes, but it must match the queries and update paths you actually use.

schema.jsJavaScript
db.createCollection("lessons", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["slug", "title", "minutes"],
      properties: {
        slug: { bsonType: "string" },
        title: { bsonType: "string" },
        minutes: { bsonType: "int", minimum: 1 }
      }
    }
  }
})

db.lessons.createIndex({ published: 1, minutes: -1 })
Your turn

Index a filtered list

Medium

Create a compound index supporting published: true and sorting by minutes descending.

JAVASCRIPT · starter
db.lessons.createIndex({})
Check your understanding before moving on.
Knowledge checkQuestion 1

In the ESR rule, what should the first fields of a compound index match?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 10 of 300 · 0% of course