Courseiva
Aggregation Framework →hardMultiple Choice

C100DEV Aggregation Framework Practice Question

An analytics collection contains order documents with an array of items. You need to output a single document for each element in the items array while preserving documents that have an empty or missing items array. Which stage configuration achieves this?

⚠ Common exam trap

Developers often forget that the default behavior of $unwind is to completely drop documents where the target array field is missing, null, or empty, leading to unexpected data loss.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

{ $unwind: { path: "$items", preserveNullAndEmptyArrays: true } }

Using the preserveNullAndEmptyArrays option within the $unwind stage ensures that parent documents lacking the target array or containing empty arrays are not dropped from the pipeline result set. This behavior is essential when performing left outer joins or maintaining complete statistical counts across documents with optional nested arrays.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    { $unwind: { path: "$items", preserveNullAndEmptyArrays: true } }

    Why this is correct

    Passing an options document to $unwind with path and preserveNullAndEmptyArrays set to true instructs MongoDB to emit documents even when the items array is null, missing, or empty, ensuring no parent records are silently filtered out.

  • ✗

    { $unwind: "$items" }, { $match: { items: { $exists: false } } }

    Why it's wrong here

    The standard $unwind stage completely drops documents with missing or empty arrays before any subsequent stages can evaluate them, making it impossible for a following $match stage to recover those discarded parent records.

  • ✗

    { $project: { items: { $ifNull: ["$items", [null]] } } }, { $unwind: "$items" }

    Why it's wrong here

    The `$ifNull` substitution only replaces a missing array, leaving genuinely empty arrays untouched; `$unwind` then drops those documents entirely, violating the preservation requirement. It is tempting because `$ifNull` is designed to supply fallback values for null or absent fields, which would be the right approach when only missing arrays, not empty ones, needed handling.

  • ✗

    { $lookup: { from: "items", localField: "itemId", foreignField: "_id", as: "items", preserveNullAndEmptyArrays: true } }

    Why it's wrong here

    $lookup performs a join against a separate collection, so it cannot emit one document per existing array element, and preserveNullAndEmptyArrays is not a valid $lookup field. $lookup suits enriching documents from related collections, not reshaping embedded arrays.

About these practice questions

This C100DEV question is part of Courseiva's 259-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official MongoDB exam blueprint

This C100DEV practice question is part of Courseiva's free MongoDB certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the C100DEV exam.