MongoDB Beginner Quiz
30 questions on MongoDB.
Question 1: What type of database is MongoDB?
- A. A relational SQL database
- B. A NoSQL document-oriented database β (correct answer)
- C. A graph database
- D. A strictly key-value store
Explanation: MongoDB is a NoSQL database that stores data in flexible, JSON-like documents rather than rigid tables and rows.
Question 2: What is the equivalent of a relational database "Table" in MongoDB?
- A. Document
- B. Database
- C. Collection β (correct answer)
- D. Array
Explanation: In MongoDB, a database contains Collections, and those Collections contain Documents.
Question 3: What is the equivalent of a relational database "Row" in MongoDB?
- A. Collection
- B. Document β (correct answer)
- C. Field
- D. Index
Explanation: A Document represents a single record of data, structured as a set of key-value pairs.
Question 4: What data format does MongoDB use to store documents behind the scenes?
- A. XML
- B. standard JSON
- C. BSON (Binary JSON) β (correct answer)
- D. CSV
Explanation: BSON extends the JSON model to provide additional data types (like Dates and ObjectIDs) and allows for highly efficient encoding and decoding.
Question 5: Which command is used to insert a single document into a MongoDB collection named users?
- A.
db.users.addOne()
- B.
db.users.insert()
- C.
db.users.insertOne() β (correct answer)
- D.
db.users.create()
Explanation: insertOne() is the modern, official method for adding a single document to a collection.
Question 6: Which command retrieves all documents from the users collection?
- A.
db.users.getAll()
- B.
db.users.select()
- C.
db.users.fetch()
- D.
db.users.find() β (correct answer)
Explanation: The find() method without any query filters returns a cursor pointing to all documents in the collection.
Question 7: How do you query for documents where the age field is exactly 25?
- A.
db.users.find({ age == 25 })
- B.
db.users.find({ age: 25 }) β (correct answer)
- C.
db.users.find(age = 25)
- D.
db.users.search(age: 25)
Explanation: MongoDB queries use JSON-like objects to specify equality filters.
Question 8: What does the unique _id field in MongoDB represent?
- A. The document size
- B. The Primary Key β (correct answer)
- C. A foreign key
- D. The index number
Explanation: MongoDB requires every document to have an _id field that uniquely identifies it within the collection. If you don't provide one, MongoDB automatically generates an ObjectId.
Question 9: Which MongoDB operator is used to filter values "Greater Than"?
- A.
$gt β (correct answer)
- B.
$greater
- C.
>
- D.
$plus
Explanation: MongoDB query operators start with a dollar sign. $gt stands for "greater than". (e.g., db.users.find({ age: { $gt: 18 } })).
Question 10: How do you update a single document where the name is "Alice"?
- A.
db.users.update(name: "Alice")
- B.
db.users.updateOne({ name: "Alice" }, { $set: { age: 26 } }) β (correct answer)
- C.
db.users.modify({ name: "Alice" })
- D.
db.users.set({ name: "Alice" })
Explanation: updateOne() takes a filter document first, and an update document second. The $set operator replaces the specified fields.
Question 11: What happens if you use updateOne without the $set operator in older MongoDB versions (using update())?
- A. It throws a syntax error
- B. It updates only the specific field
- C. It completely overwrites/replaces the entire document with the new fields β (correct answer)
- D. It deletes the document
Explanation: Omitting $set replaces the whole document. Modern Drivers use updateOne() with $set to prevent accidental overwrites, or replaceOne() if full replacement is intended.
Question 12: Which command deletes exactly one document matching the condition?
- A.
db.users.removeOne()
- B.
db.users.deleteOne() β (correct answer)
- C.
db.users.drop()
- D.
db.users.clear()
Explanation: deleteOne() removes the first document that matches the filter criteria.
Question 13: What does the db.collection.drop() command do?
- A. Deletes a single document
- B. Drops the database connection
- C. Completely removes the collection and all its documents and indexes β (correct answer)
- D. Hides the collection
Explanation: Dropping a collection entirely removes it from the database, which is much faster than deleting documents one by one.
Question 14: Which MongoDB tool provides a graphical user interface (GUI) to view and query data easily?
- A. MongoDB Atlas
- B. MongoDB Compass β (correct answer)
- C. MongoDB Shell
- D. MongoDB Engine
Explanation: MongoDB Compass is the official GUI tool that allows developers to visualize schemas, run queries, and manage indexes without using the command line.
Question 15: What is MongoDB Atlas?
- A. A mapping tool
- B. MongoDB's fully-managed cloud database service (DBaaS) β (correct answer)
- C. A query language
- D. An indexing algorithm
Explanation: Atlas allows you to host MongoDB databases on AWS, Google Cloud, or Azure without managing the underlying servers yourself.
Question 16: In MongoDB, how do you sort the results of a query by age in descending order?
- A.
db.users.find().sort({ age: -1 }) β (correct answer)
- B.
db.users.find().sort({ age: "DESC" })
- C.
db.users.find().order({ age: -1 })
- D.
db.users.find({ $sort: { age: -1 } })
Explanation: The .sort() cursor method accepts an object where 1 means ascending and -1 means descending.
Question 17: Which MongoDB operator checks if an array field contains a specific value?
- A.
$in β (correct answer)
- B.
$contains
- C.
$has
- D.
$array
Explanation: The $in operator matches any of the values specified in an array.
Question 18: What is the MongoDB Aggregation Pipeline?
- A. A tool to backup databases
- B. A framework for data aggregation modeled on the concept of data processing pipelines (using stages like
$match, $group, $sort) β (correct answer)
- C. A network connection type
- D. A GUI tool
Explanation: The aggregation pipeline allows you to perform complex data transformations and analytics by passing documents through multiple stages.
Question 19: In an aggregation pipeline, what does the $match stage do?
- A. Joins two collections
- B. Groups data together
- C. Filters documents to pass only the matching documents to the next pipeline stage β (correct answer)
- D. Sorts the documents
Explanation: $match works exactly like the standard find() query filter, removing irrelevant data early in the pipeline.
Question 20: In an aggregation pipeline, what does the $group stage do?
- A. Filters out NULL values
- B. Groups input documents by a specified
_id expression and applies accumulator expressions (like $sum or $avg) to each group β (correct answer)
- C. Sorts data alphabetically
- D. Inserts new documents into groups
Explanation: $group is the MongoDB equivalent of the SQL GROUP BY clause.
Question 21: How do you limit the output of a find() query to only return the name field and exclude the _id field? (This is called Projection)
- A.
db.users.find({}, { name: 1, _id: 0 }) β (correct answer)
- B.
db.users.find({ name: true, _id: false })
- C.
db.users.project({ name: 1 })
- D.
db.users.find().select("name")
Explanation: The second argument in find() is the projection document. 1 includes the field, 0 explicitly excludes it.
Question 22: What does the $exists operator do?
- A. Checks if the database is online
- B. Matches documents that contain the specified field, even if the value is null β (correct answer)
- C. Drops a collection
- D. Checks if an index is present
Explanation: Because MongoDB schemas are flexible, documents might be missing certain fields entirely. $exists allows you to query for the presence or absence of a field.
Question 23: Which operator is used to increment a numerical value in a document?
- A.
$add
- B.
$plus
- C.
$inc β (correct answer)
- D.
$increment
Explanation: db.users.updateOne({ _id: 1 }, { $inc: { age: 1 } }) will mathematically add 1 to the current value of the age field.
Question 24: How do you create an ascending index on the email field?
- A.
db.users.addIndex({ email: 1 })
- B.
db.users.createIndex({ email: 1 }) β (correct answer)
- C.
db.users.index({ email: true })
- D.
db.users.makeIndex("email")
Explanation: Indexes drastically improve read performance. 1 creates an ascending index, -1 creates descending.
Question 25: What is a "Replica Set" in MongoDB?
- A. A group of identical collections
- B. A group of MongoDB processes that maintain the same data set, providing high availability and redundancy β (correct answer)
- C. A backup file
- D. A GUI feature
Explanation: Replica sets consist of a Primary node (handles writes) and Secondary nodes (replicate data and can handle reads). If the primary fails, a secondary takes over.
Question 26: What is "Sharding" in MongoDB?
- A. Deleting old data automatically
- B. The process of storing data records across multiple machines to support deployments with very large data sets and high throughput operations β (correct answer)
- C. Encrypting data
- D. Compressing BSON documents
Explanation: Sharding is MongoDB's approach to horizontal scaling.
Question 27: Can MongoDB enforce validation rules (Schemas) on a collection?
- A. No, MongoDB is completely schemaless
- B. Yes, using JSON Schema Validation applied to the collection β (correct answer)
- C. Only if you use SQL
- D. Only on the
_id field
Explanation: While inherently flexible, modern MongoDB allows you to enforce strict validation rules (like requiring a string for emails) using JSON Schema rules.
Question 28: Which operator adds elements to an array only if they do not already exist in that array?
- A.
$push
- B.
$addToSet β (correct answer)
- C.
$append
- D.
$insert
Explanation: $push adds the value no matter what, causing duplicates. $addToSet treats the array like a Set, preventing duplicate entries.
Question 29: What is the $lookup stage in the Aggregation Pipeline?
- A. It searches the internet
- B. It performs a left outer join to an unsharded collection in the same database to filter in documents from the "joined" collection β (correct answer)
- C. It finds a specific string
- D. It returns the database metadata
Explanation: $lookup is MongoDB's equivalent of a SQL LEFT JOIN, allowing you to combine data from multiple collections.
Question 30: What is an "Embedded Document" (or Subdocument)?
- A. A document saved as a PDF
- B. A document stored directly within another document as a field value β (correct answer)
- C. A linked document in another collection
- D. An encrypted document
Explanation: Instead of relying on JOINs, MongoDB encourages "embedding" related data (like a user's address object) directly inside the parent document for faster read times.