Courseiva
Indexing →mediumMultiple Select

C100DEV Indexing Practice Question

You are designing indexes for a MongoDB collection that stores user profiles. The collection has a field 'email' that must be unique, and a field 'username' that is also unique. You also need to support queries that find users by a combination of 'country' and 'city'. Which TWO of the following statements are correct regarding index creation and behavior in this scenario? (Choose two.)

⚠ Common exam trap

A common mix-up: candidates confuse compound index prefix support with the ability to use any field, and misunderstanding that a compound unique index enforces uniqueness on the combination, not on each field separately.

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

✓

Creating a unique index on email will prevent insertion of documents with duplicate email values, but will allow multiple documents with missing email field only if the index is sparse or partial.

The scenario requires unique constraints on email and username, and query support for country and city. A unique index on email allows multiple missing emails only if sparse or partial, so that statement is correct. A compound index on country and city supports queries on the leading field country and on both fields together, making that statement correct. The other statements misstate index prefix behavior, compound uniqueness, or the scope of unique indexes.

Answer analysis

Option-by-option breakdown

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

  • ✓

    Creating a unique index on email will prevent insertion of documents with duplicate email values, but will allow multiple documents with missing email field only if the index is sparse or partial.

    Why this is correct

    A unique index enforces uniqueness on the indexed field, but by default it treats missing fields as null and allows only one document without the field. To allow multiple documents missing the email field while still enforcing uniqueness when present, you must create the index with the sparse option or a partial filter expression. This is a common requirement for optional unique fields.

  • ✗

    A compound index on { country: 1, city: 1 } can support queries that filter on city alone, because MongoDB can use the index prefix from the right side.

    Why it's wrong here

    MongoDB compound indexes support queries that use a prefix of the index fields, starting from the leftmost field. An index on { country: 1, city: 1 } can support queries on country alone or on both country and city, but not on city alone. To support city-only queries, you would need a separate index with city as the leading field. This statement incorrectly describes index prefix usage.

  • ✓

    A compound index on { country: 1, city: 1 } can be used for a query that filters on both country and city, and also for a query that filters on country alone.

    Why this is correct

    A compound index supports queries that use a prefix of its fields. With { country: 1, city: 1 }, the index can be used for queries filtering on country, or on both country and city. The leading field country is the prefix, so country-only queries can use the index efficiently. Queries on city alone cannot use it as a prefix, but that is not part of this statement.

  • ✗

    Creating a unique index on username will automatically create a unique index on email if both are specified in the same createIndex command using a compound key pattern.

    Why it's wrong here

    A compound unique index on { username: 1, email: 1 } enforces uniqueness across the combination of username and email, not on each field individually. Two documents could have the same username with different emails, or the same email with different usernames, and still satisfy the compound uniqueness. Separate unique indexes are required to enforce uniqueness independently on each field.

  • ✗

    Unique indexes in MongoDB enforce uniqueness across all fields in the document, not just the indexed field, to prevent duplicate documents.

    Why it's wrong here

    Unique indexes enforce uniqueness only on the specified key pattern. They do not compare entire documents. For example, a unique index on email only prevents two documents from having the same email value; other fields can be identical. To prevent duplicate documents entirely, you would need a unique index on all fields or a different mechanism, which is not how unique indexes work.

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.