MongoDB interview questions

15 questions from the MongoDB Fundamentals: Documents, Queries and Indexes course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 2: Documents & Collections

1. What happens if you insert a document without an _id?

  1. The insert fails
  2. _id is left null
  3. MongoDB auto-generates an ObjectId
  4. It stores the document in a trash collection
Show answer

Answer: MongoDB auto-generates an ObjectId

MongoDB always ensures a unique _id, generating an ObjectId when one isn't provided.

Lesson: Collections & the _id Field

Module 4: Reading Data

2. Which operator checks if a field's value is in a given list?

  1. $eq
  2. $gt
  3. $exists
  4. $in
Show answer

Answer: $in

$in matches if the field's value equals any item in the array.

Lesson: Query Operators

Module 5: Updating Data

3. Which operator increments a numeric field?

  1. $inc
  2. $set
  3. $push
  4. $unset
Show answer

Answer: $inc

$inc atomically adds (or subtracts) a value from a number field.

Lesson: Update Operators

Module 8: Aggregation

4. What does the $group stage do?

  1. Filters out unwanted documents
  2. Aggregates documents by a key, computing values like sums
  3. Sorts documents in ascending order
  4. Creates an index on a field
Show answer

Answer: Aggregates documents by a key, computing values like sums

$group buckets documents by an _id expression and computes accumulators like $sum, $avg per bucket.

Lesson: An Aggregation Example

Module 10: Using Mongoose

5. What happens if a required field is missing when saving a Mongoose document?

  1. MongoDB silently fills in a default
  2. The field is saved as null
  3. A ValidationError is thrown and nothing is saved
  4. The document is saved with a warning
Show answer

Answer: A ValidationError is thrown and nothing is saved

Mongoose validates before hitting the database — a required-field violation rejects the save.

Lesson: Mongoose Validation

Module 11: Reliability

6. What problem does sharding primarily solve?

  1. Automatic failover when the primary goes down
  2. Validating document structure
  3. Formatting JSON output
  4. Scaling storage and throughput beyond a single server
Show answer

Answer: Scaling storage and throughput beyond a single server

Sharding horizontally scales out data and load; failover is handled by replica sets instead.

Lesson: Sharding

Module 12: Cloud

7. What format does MongoDB use to store documents?

  1. XML
  2. CSV
  3. BSON
  4. YAML
Show answer

Answer: BSON

BSON extends JSON with types such as Date and ObjectId.

Lesson: MongoDB Atlas at a Glance

8. Which method adds one document?

  1. addDoc
  2. push
  3. create
  4. insertOne
Show answer

Answer: insertOne

insertOne creates the collection if needed.

Lesson: MongoDB Atlas at a Glance

9. Which operator appends to an array field?

  1. $push
  2. $set
  3. $inc
  4. $unset
Show answer

Answer: $push

$push adds a value to an array.

Lesson: MongoDB Atlas at a Glance

10. What does an IXSCAN in explain() show?

  1. A full collection scan
  2. An index was used
  3. A syntax error
  4. An empty result
Show answer

Answer: An index was used

COLLSCAN is the slow full scan.

Lesson: MongoDB Atlas at a Glance

11. When should you embed related data?

  1. When it grows without limit
  2. When it is shared by many parents
  3. When it is read together and stays small
  4. Never
Show answer

Answer: When it is read together and stays small

Embedding gives one fast read for bounded data.

Lesson: MongoDB Atlas at a Glance

12. What does the $group stage do?

  1. Filters documents
  2. Deletes documents
  3. Renames fields
  4. Aggregates documents by a key
Show answer

Answer: Aggregates documents by a key

$group computes sums, counts and averages per key.

Lesson: MongoDB Atlas at a Glance

13. What does a replica set provide?

  1. High availability through copies and failover
  2. More storage per node
  3. Schema validation
  4. Faster inserts
Show answer

Answer: High availability through copies and failover

A secondary is promoted if the primary fails.

Lesson: MongoDB Atlas at a Glance

14. Which problem does sharding mainly solve?

  1. Password resets
  2. Scaling storage and throughput
  3. Date formatting
  4. Backups only
Show answer

Answer: Scaling storage and throughput

Shards split data across servers by shard key.

Lesson: MongoDB Atlas at a Glance

← Back to the course