






















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This practice exam evaluates the candidate’s ability to design, build, and manage MongoDB applications. It covers topics such as schema design, query optimization, aggregation framework, indexing, and data modeling. This exam is ideal for developers working with MongoDB to create high-performance applications.
Typology: Exams
1 / 94
This page cannot be seen from the preview
Don't miss anything!























































































Question 1. Which BSON type is used to store binary data such as images or files? A) String B) Binary C) Date D) ObjectId Answer: B Explanation: BSON’s Binary type stores arbitrary binary data, making it suitable for raw bytes like images, unlike String which stores UTF‑8 text. Question 2. What is the default index created on every MongoDB collection? A) Single‑field index on the first field B) Compound index on all fields C) _id index D) Text index on all string fields Answer: C Explanation: MongoDB automatically creates a unique index on the _id field for each collection to ensure document identity and fast lookup. Question 3. Which method inserts multiple documents in a single operation? A) insertOne() B) insertMany() C) save() D) updateOne() Answer: B Explanation: insertMany() accepts an array of documents and writes them to the collection in one batch, reducing round‑trips.
Question 4. In a query, how would you select documents where the field “age” is greater than 30? A) { age: { $gt: 30 } } B) { age: { $lt: 30 } } C) { age: { $eq: 30 } } D) { age: { $ne: 30 } } Answer: A Explanation: The $gt (greater than) operator matches values strictly greater than the specified number. Question 5. When updating a document, which operator adds a new element to an array only if it does not already exist? A) $push B) $addToSet C) $pop D) $pull Answer: B Explanation: $addToSet ensures uniqueness within an array, preventing duplicate entries, while $push always appends. Question 6. Which of the following statements about ObjectId is true? A) It is always generated by the client application. B) It contains a 12‑byte value with a timestamp, machine identifier, process id, and counter. C) It can be a string of any length. D) It must be manually supplied for every insert.
C) background: true D) expireAfterSeconds: 3600 Answer: B Explanation: A sparse index excludes documents that lack the indexed field, reducing index size and avoiding null entries. Question 10. In aggregation, which stage should generally be placed first to reduce the amount of data processed by later stages? A) $project B) $group C) $match D) $sort Answer: C Explanation: $match filters documents early, minimizing the volume of data that subsequent stages need to handle, which improves performance. Question 11. Which of the following best describes a “multikey” index? A) An index on multiple fields combined. B) An index that supports array fields by indexing each array element separately. C) An index that is created in the background. D) An index that enforces uniqueness across several fields. Answer: B Explanation: A multikey index is automatically created when indexing an array field; each element of the array is indexed individually. Question 12. When using the $lookup stage, what type of join does MongoDB perform?
A) Inner join B) Right outer join C) Left outer join D) Full outer join Answer: C Explanation: $lookup implements a left outer join, returning all documents from the left (local) collection and matching documents from the right (foreign) collection. Question 13. Which of the following is a valid reason to embed a document rather than reference it? A) The embedded data changes frequently and is large. B) The embedded data is always accessed together with the parent document. C) The embedded data must be shared across many unrelated documents. D) The embedded data exceeds 16 MB. Answer: B Explanation: Embedding is ideal when related data is read together, reducing the need for additional lookups. Question 14. What does the explain("executionStats") output provide? A) Only the query planner’s chosen plan. B) Detailed statistics about index usage, number of documents examined, and execution time. C) The raw BSON of the returned documents. D) A list of all collections in the database. Answer: B Explanation: executionStats adds runtime details such as nReturned, totalDocsExamined, and executionTimeMillis, useful for performance tuning.
D) $push Answer: B Explanation: $avg computes the arithmetic mean of the supplied numeric expression across the grouped documents. Question 18. What is the effect of creating an index in the background? A) The index is created instantly without any I/O. B) Index creation blocks all read and write operations. C) The server continues to serve reads and writes while the index builds. D) The index is only visible to secondary members. Answer: C Explanation: Background index builds allow the database to remain operational, though they may take longer to complete. Question 19. Which of the following JSON Schema keywords can be used to enforce that a field “status” only contains the values “A”, “B”, or “C”? A) pattern B) enum C) maximum D) minLength Answer: B Explanation: The enum keyword restricts a field’s value to a predefined set of allowed literals. Question 20. When should you use the $unwind stage in a pipeline? A) To merge two collections. B) To deconstruct an array field into separate documents.
C) To limit the number of returned documents. D) To sort documents by a computed field. Answer: B Explanation: $unwind creates a separate output document for each element of an array field, enabling further processing of individual elements. Question 21. What does the $addFields stage do in an aggregation pipeline? A) Removes existing fields from documents. B) Adds new fields or updates existing fields with computed values. C) Filters documents that match a condition. D) Groups documents by a specified key. Answer: B Explanation: $addFields adds or overwrites fields in each document, similar to $project but without excluding existing fields. Question 22. Which operation would you use to delete all documents where “status” equals “inactive”? A) db.collection.remove({ status: "inactive" }) B) db.collection.deleteOne({ status: "inactive" }) C) db.collection.drop() D) db.collection.updateMany({ status: "inactive" }, { $set: { deleted: true } }) Answer: A Explanation: remove() (or deleteMany()) with a filter deletes every matching document; deleteOne would remove only a single match. Question 23. In MongoDB, which of the following is NOT a valid BSON data type?
Explanation: A compound index can cover both the filter and sort patterns of a query, enabling a covered query and eliminating additional sorting steps. Question 26. Which of the following is true about the $project stage? A) It can only include fields, not exclude them. B) It can rename fields, compute new fields, and exclude existing ones. C) It must be the first stage in a pipeline. D) It cannot be used after a $group stage. Answer: B Explanation: $project reshapes each document by adding, removing, or renaming fields and can also compute expressions. Question 27. When modeling a many‑to‑many relationship, which pattern is generally recommended? A) Store an array of ObjectIds in both related collections (two‑way referencing). B) Embed the entire related documents in each side. C) Use a separate linking collection that contains references to both sides. D) Duplicate all data in a single collection. Answer: C Explanation: A linking (junction) collection keeps references to both sides, allowing flexible queries without massive duplication. Question 28. Which of the following statements about GridFS is correct? A) GridFS stores files as a single document regardless of size. B) GridFS splits files into chunks of up to 255 KB and stores them in fs.chunks. C) GridFS can only be used for image files.
D) GridFS automatically compresses stored files. Answer: B Explanation: GridFS divides a file into 255 KB chunks (default) stored in the fs.chunks collection, with metadata in fs.files. Question 29. What does the readPreference “primaryPreferred” do? A) Always reads from the primary, never from secondaries. B) Reads from a secondary if the primary is unavailable; otherwise, reads from primary. C) Randomly chooses any member of the replica set. D) Reads only from secondaries. Answer: B Explanation: primaryPreferred prefers the primary but falls back to secondaries when the primary cannot be reached. Question 30. Which aggregation operator can be used to concatenate strings from multiple documents? A) $sum B) $push C) $addToSet D) $concat Answer: D Explanation: $concat (used within a $group $push or $addFields expression) concatenates string values; $push only creates an array. Question 31. If you need to enforce that a field “email” is unique across a collection, which index option should you use?
Explanation: upsert:true creates a new document that combines the filter fields and the update operators when no match is found. Question 34. Which of the following best describes a “covered query”? A) A query that returns all fields of the matching documents. B) A query that can be satisfied entirely using an index without reading the documents. C) A query that uses a $lookup stage. D) A query that requires a full collection scan. Answer: B Explanation: A covered query accesses only the index (both filter and projection fields), avoiding fetching full documents and thus improving performance. Question 35. What is the effect of the $push operator with the $each modifier? A) It adds a single element to an array. B) It adds multiple elements to an array in one operation. C) It removes duplicate elements from an array. D) It sorts the array after insertion. Answer: B Explanation: $push: { field: { $each: [elem1, elem2] } } appends each element in the $each array to the target array. Question 36. Which of the following statements about the $sort stage is true? A) $sort can only be used after $group. B) $sort must be the first stage in a pipeline. C) $sort can use an index to avoid an in‑memory sort if the preceding stage is $match and the sort keys are indexed.
D) $sort automatically removes duplicate documents. Answer: C Explanation: When the pipeline’s $match filters on indexed fields and the sort keys are part of the same index, MongoDB can use the index to produce sorted results without extra memory. Question 37. In a sharded cluster, which field must be included in every query to guarantee targeted routing? A) The _id field. B) The shard key. C) The primary key. D) The readPreference field. Answer: B Explanation: Including the shard key in the query allows the mongos router to direct the operation to the appropriate shard, avoiding scatter‑gather. Question 38. Which of the following is a valid way to limit the number of documents returned by a find() operation? A) db.collection.find().skip(10) B) db.collection.find().limit(5) C) db.collection.find({ $limit: 5 }) D) db.collection.find().count(5) Answer: B Explanation: The limit() cursor method restricts the result set to the specified number of documents. Question 39. What does the $setOnInsert operator do?
Question 42. Which of the following statements about the $lookup stage’s “localField” and “foreignField” is correct? A) Both fields must be arrays. B) They must have the same data type for matching. C) They can reference fields in embedded documents only. D) They are optional; $lookup works without them. Answer: B Explanation: For $lookup to match documents, the values of localField and foreignField must be comparable, typically of the same BSON type. Question 43. What is the purpose of the “expireAfterSeconds” option in an index? A) To enforce uniqueness for a time window. B) To automatically delete documents after a specified age. C) To delay index creation by the given number of seconds. D) To set a TTL on the index itself. Answer: B Explanation: A TTL (time‑to‑live) index removes documents whose indexed field value is older than the specified number of seconds. Question 44. Which aggregation operator can be used to calculate a running total across sorted documents? A) $sum B) $group with $push C) $setWindowFields with $sum as a window function D) $map Answer: C
Explanation: $setWindowFields introduced in MongoDB 5.0 enables window functions like $sum to compute cumulative totals over a defined partition and sort order. Question 45. When should you use the $replaceOne() method instead of $updateOne()? A) When you want to modify only a single field. B) When you need to replace the entire document, optionally preserving the _id. C) When you want to add an array element. D) When you need to upsert with partial updates. Answer: B Explanation: $replaceOne replaces the whole document with the supplied replacement document; $updateOne applies field‑level operators. Question 46. Which of the following statements about the $addToSet operator is false? A) It adds an element to an array only if it does not already exist. B) It can accept the $each modifier to add multiple elements. C) It maintains the order of insertion. D) It removes duplicate entries automatically from the entire collection. Answer: D Explanation: $addToSet only prevents duplicates within the target array of the updated document; it does not affect other documents. Question 47. What does the $out stage do in an aggregation pipeline? A) Returns the pipeline results to the client. B) Writes the pipeline results to a new collection, replacing it if it exists. C) Sends results to a remote server via HTTP. D) Logs the results to the server log.
B) The unique identifier for each output document, defined by the grouping key(s). C) The index key used for grouping. D) The number of documents in the group. Answer: B Explanation: In $group, _id defines the grouping criteria; each distinct _id value produces a separate output document. Question 51. Which of the following is true about the $inc operator when applied to a non‑numeric field? A) It silently creates the field with value 0. B) It throws a runtime error. C) It converts the field to a string. D) It does nothing. Answer: B Explanation: $inc expects a numeric field; applying it to a non‑numeric type results in a BSON type mismatch error. Question 52. When using a compound index { a: 1, b: 1, c: 1 }, which of the following queries can use the index for sorting without an in‑memory sort? A) sort({ c: 1 }) B) sort({ a: 1, b: 1 }) C) sort({ b: - 1, a: 1 }) D) sort({ a: 1, c: 1 }) Answer: B Explanation: The index order must match the sort order from the leading field onward; sorting on a and b respects the index sequence.
Question 53. Which method returns a cursor that can be iterated to fetch documents one at a time? A) db.collection.findOne() B) db.collection.aggregate() C) db.collection.find() D) db.collection.count() Answer: C Explanation: find() returns a cursor object; findOne() returns a single document, aggregate() returns a cursor as well but the primary method for standard queries is find(). Question 54. What is the purpose of the $text index type? A) To enable full‑text search on string fields. B) To index numeric ranges. C) To store binary data. D) To enforce uniqueness across multiple fields. Answer: A Explanation: $text indexes support language‑aware search of string content, allowing $search queries. Question 55. Which of the following statements about the $lookup “pipeline” form (available in MongoDB 5.0+) is correct? A) It can only perform equality matches. B) It allows you to specify an aggregation pipeline to filter the foreign collection. C) It replaces the need for $match stages in the main pipeline. D) It cannot be combined with $unwind.