Ch. 12 · MongoDB

MongoDB Missing Fields Versus Null

MongoDB Missing Fields Versus Null. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

Missing fields and explicit null values represent different states. Queries and indexes may treat them differently, so specify intended matching behavior.

Before you start

You should understand documents, collections and indexes. Sketch representative documents and the reads and writes they must support. MongoDB-specific examples assume a collection with the shown fields; deployment topology, permissions and existing indexes can affect the operational behavior being discussed.

The practical goal is to reason through this situation: A record without archivedAt differs from one explicitly storing null. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: List the three states

Distinguish absent archivedAt, explicit null and a timestamp.

Step 2: Choose matching semantics

A null equality query can match both missing and null.

Step 3: Use presence explicitly

Use $exists when the requirement concerns absence rather than value.

Worked scenario

A record without archivedAt differs from one explicitly storing null.

db.records.find({ archivedAt: { $exists: false } });
JavaScript

In mongosh this selects documents lacking the field. It differs from { archivedAt: null }, which also includes explicit null. A timestamp represents another state, so seed all three rather than relying on one example.

Common mistake

A simple null query can include both missing and null fields.

Verify the behavior

Compare absence, null and populated queries against those three seeded documents.

Interview exercise

Match only absent fields.

Answer and reasoning

Use an existence condition and test all three cases: absent, explicit null and a populated value.

Continue learning

Compare the scenario with the MongoDB interview questions and test your understanding with the MongoDB MCQs. For terminology and implementation details, consult the reference material.

More in MongoDB

read ✓MongoDB · mid

MongoDB Bulk Writes

Batch inserts and updates with bulkWrite, choose ordered or unordered, and handle partial failures correctly.

~2 min readread →
read ✓MongoDB · mid

MongoDB Capped Collections

Use capped collections for fixed-size, insertion-ordered logs, and know why documents cannot grow.

~2 min readread →
read ✓MongoDB · hard

MongoDB Change Streams and Resume Tokens

React to inserts and updates with change streams, persist resume tokens, and handle invalidate and failover correctly.

~2 min readread →
esc