Skip to content
Learn Netverks

Lesson

Step 13/36 36% through track

validators-mongodb

Document validators

Last reviewed Jun 1, 2026 Content v20260601
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Document validators: document modeling, query operators, and aggregation patterns for MongoDB.

Teams query Document validators on every MongoDB codebase—skipping it leaves gaps in debugging and data reviews.

You will apply Document validators in contexts like: Content catalogs, event logs, mobile sync backends, and polyglot stacks beside SQL services.

Copy JavaScript shell queries from each lesson into mongosh or MongoDB Atlas Data Explorer—inspect matched documents and explain plans. The in-browser lab (execution_profile: none) ships later; mongosh is the practice path now.

When you can explain the previous lesson's ideas without copying example queries verbatim.

collMod with validator enforces JSON Schema rules on insert/update—middle ground between rigid SQL and no rules.

Create validator

db.createCollection('invoices', {
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      required: ['customerId', 'total', 'issuedAt'],
      properties: {
        customerId: { bsonType: 'string' },
        total: { bsonType: ['int', 'double', 'decimal'] },
        issuedAt: { bsonType: 'date' }
      }
    }
  },
  validationLevel: 'moderate'
})

Practice: Run on practice in mongosh.

validationAction

error rejects bad writes; warn logs in older patterns—prefer error in production.

Important interview questions and answers

  1. Q: Validator vs app-only checks?
    A: DB-side validation blocks rogue scripts and ad-hoc shells.
  2. Q: moderate vs strict?
    A: moderate validates updates only if they touch validated fields.

Self-check

  1. What does $jsonSchema enforce?
  2. Name three required invoice fields in the example.

Tip: Validators catch bad writes from shells and legacy scripts—not only app code.

Interview prep

$jsonSchema?
Declares required fields and BSON types on writes.
validationLevel moderate?
Validates updates touching validated fields.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • $jsonSchema when?
  • moderate level?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump