Unit 2: MongoDB Basics and CRUD Operations - Practice Quiz

CSE494 — Intelligent Nosql Databases 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What does a MongoDB database primarily contain?

Databases Easy
A. Stored procedures only
B. Collections
C. HTML pages
D. Worksheets

2 Which MongoDB shell command displays the name of the currently selected database?

Databases Easy
A. show dbs
B. db
C. list databases with all configuration details
D. db.current()

3 What is a collection in MongoDB?

Collections Easy
A. A single BSON value
B. A group of documents
C. A fixed table that must always enforce the same columns
D. A database server

4 A MongoDB collection is most similar to which concept in a relational database?

Collections Easy
A. A trigger
B. A database connection
C. A row
D. A table

5 How is data organized inside a MongoDB document?

Documents Easy
A. As rows and joins
B. As HTML elements
C. As field-value pairs
D. As a sequence of database administration commands

6 Which field is automatically used as the unique identifier of a MongoDB document?

Documents Easy
A. primary_identifier_value
B. _id
C. _key
D. id_

7 Which operation adds a single document to a MongoDB collection?

Documents Easy
A. insertOne()
B. findOne()
C. createOneDocumentAndAutomaticallyBuildEveryPossibleIndex()
D. updateOne()

8 Which method is commonly used to retrieve documents from a MongoDB collection?

Documents Easy
A. replaceCollection()
B. drop()
C. find()
D. insertMany()

9 What does BSON stand for?

BSON Data Types Easy
A. Binary Storage Object Network
B. Basic JSON
C. Binary JSON
D. Buffered SQL Object Notation

10 Which BSON data type stores a true or false value?

BSON Data Types Easy
A. A numeric integer restricted to the values zero and one
B. ObjectId
C. String
D. Boolean

11 Which BSON data type is commonly generated for the _id field by MongoDB?

BSON Data Types Easy
A. Timestamp
B. RegularExpression
C. ObjectId
D. Decimal128

12 Which BSON data type should be used to store a list of values in one field?

BSON Data Types Easy
A. A nested database containing multiple independent collections
B. Array
C. Boolean
D. Date

13 Which MongoDB shell command selects a database named school?

Creating and Dropping Databases and Collections Easy
A. use school
B. open school
C. db.createCollection("school")
D. select school

14 When is a newly selected MongoDB database normally created permanently?

Creating and Dropping Databases and Collections Easy
A. When the shell starts
B. When use is entered
C. When data is first stored
D. When every collection and its complete validation schema have been declared

15 Which command creates a collection named students explicitly?

Creating and Dropping Databases and Collections Easy
A. db.createCollection("students")
B. db.newCollection("students")
C. db.students.createDatabase()
D. create students

16 Which command removes the currently selected MongoDB database?

Creating and Dropping Databases and Collections Easy
A. db.drop()
B. db.dropDatabase()
C. db.removeEveryCollectionBeforeClosingTheDatabase()
D. db.deleteDatabase()

17 Which command removes a collection named students?

Creating and Dropping Databases and Collections Easy
A. db.students.remove()
B. db.students.deleteAllDocumentsAndRetainTheCollection()
C. db.dropDatabase()
D. db.students.drop()

18 What is embedding in MongoDB data modeling?

Data Modeling in MongoDB Easy
A. Storing related data inside one document
B. Storing every value in a separate database
C. Creating an index for each document
D. Converting every collection into a fixed relational table with mandatory columns

19 What is a reference in MongoDB data modeling?

Data Modeling in MongoDB Easy
A. An index that automatically combines all databases on the server
B. A copy of every related collection
C. A link to data in another document
D. A rule that deletes duplicate fields

20 What is the main purpose of an index in MongoDB?

Indexes and Query Optimization Easy
A. To encrypt collections
B. To speed up queries
C. To rename documents
D. To duplicate all documents across every database on the server

21 A developer runs use inventory in mongosh, but inventory does not appear in show dbs. What action will make the database appear?

Databases Medium
A. Run db.createDatabase() once
B. Create a user for the database
C. Insert a document into a collection
D. Restart the MongoDB server

22 Which command explicitly creates an orders collection with a JSON Schema validator?

Creating and Dropping Databases and Collections Medium
A. db.createCollection("orders", { validator: schema })
B. db.orders.create({ validator: schema })
C. db.orders.insertCollection({ validator: schema })
D. db.createDatabase("orders", { validator: schema })

23 An administrator has selected the test_archive database and wants to permanently remove that database. Which command should be executed?

Creating and Dropping Databases and Collections Medium
A. db.dropAllCollections()
B. db.deleteDatabase()
C. db.dropDatabase()
D. db.drop()

24 A collection must reject documents whose quantity field is not an integer. Which MongoDB feature is most appropriate?

Collections Medium
A. A JSON Schema validator
B. A text search index
C. A compound query filter
D. A capped collection option

25 Given { customer: { address: { city: "Pune" } } }, which filter correctly finds documents where the nested city is Pune?

Documents Medium
A. { "customer.address.city": "Pune" }
B. { "customer->address->city": "Pune" }
C. { customer: "address.city.Pune" }
D. { customer[address][city]: "Pune" }

26 A document contains scores: [{ subject: "Math", mark: 72 }, { subject: "Science", mark: 91 }]. Which filter requires subject: "Math" and mark: { $gt: 80 } to match the same array element?

Documents Medium
A. { "scores.subject": "Math", "scores.mark": { $gt: 80 } }
B. { scores: { $all: ["Math", 80] } }
C. { scores: { subject: "Math", mark: { $gt: 80 } } }
D. { scores: { $elemMatch: { subject: "Math", mark: { $gt: 80 } } } }

27 Which update increments an existing stock value by 5 without replacing the rest of the document?

Documents Medium
A. db.items.updateOne({ sku: "A1" }, { stock: 5 })
B. db.items.updateOne({ sku: "A1" }, { $push: { stock: 5 } })
C. db.items.updateOne({ sku: "A1" }, { $inc: { stock: 5 } })
D. db.items.updateOne({ sku: "A1" }, { $set: { stock: 5 } })

28 A financial application must store 19.99 without typical binary floating-point rounding. Which BSON value is most appropriate?

BSON Data Types Medium
A. NumberInt("19.99")
B. NumberDecimal("19.99")
C. NumberLong("19.99")
D. Double("19.99")

29 A query must compare event times chronologically and use a date index efficiently. How should new event times normally be stored?

BSON Data Types Medium
A. As BSON Date values
B. As formatted string values
C. As JavaScript code values
D. As embedded time documents

30 A collection uses MongoDB's default _id values. Which statement about an ObjectId is correct?

BSON Data Types Medium
A. It is a 16-byte BSON value containing only random data
B. It is an 8-byte BSON value generated from the document size
C. It is a 24-byte BSON value containing the collection name
D. It is a 12-byte BSON value containing a timestamp component

31 A field can contain either an integer or a numeric string. Which query selects only documents where rating is stored as a string?

BSON Data Types Medium
A. { rating: { $cast: "string" } }
B. { rating: { $format: "string" } }
C. { rating: { $type: "string" } }
D. { rating: { $value: "string" } }

32 An order's line items are always displayed with the order, are limited in number, and should remain as they were when purchased. Which model is most suitable?

Data Modeling in MongoDB Medium
A. Reference every line item by a shared key
B. Store the line items as collection metadata
C. Embed the line items in the order
D. Store each line item in a new database

33 Millions of comments belong to articles, and comments are frequently paginated and updated independently. Which modeling choice is most appropriate?

Data Modeling in MongoDB Medium
A. Duplicate every article inside each comment
B. Store comments separately with an article reference
C. Store all comments inside one global document
D. Embed every comment inside its article

34 A user's shipping address and profile settings must be updated atomically in a single operation. Which design best supports this requirement?

Data Modeling in MongoDB Medium
A. Embed both values in the user document
B. Write each value to a capped collection
C. Place each value in a separate database
D. Store each value in a separate collection

35 A frequent query is find({ status: "PAID" }).sort({ createdAt: -1 }). Which index best supports both filtering and sorting?

Indexes and Query Optimization Medium
A. { createdAt: -1, status: 1 }
B. { status: -1, createdAt: 1 }
C. { createdAt: 1, status: -1 }
D. { status: 1, createdAt: -1 }

36 Given a compound index { category: 1, price: 1, rating: -1 }, which query uses the index's longest valid prefix?

Indexes and Query Optimization Medium
A. find({ price: { $lt: 30 }, rating: 5 })
B. find({ rating: 5, price: { $lt: 30 } })
C. find({ rating: 5 })
D. find({ category: "Book", price: { $lt: 30 } })

37 An index { email: 1, name: 1 } exists. Which query can be covered by this index without fetching documents, assuming _id is not needed?

Indexes and Query Optimization Medium
A. find({ email: "a@x.com" }, { name: 1, _id: 0 })
B. find({ email: "a@x.com" }, { address: 1, _id: 0 })
C. find({ name: "Ana" }, { address: 1, _id: 0 })
D. find({ address: "Pune" }, { email: 1, _id: 0 })

38 A query is unexpectedly slow. Which command provides actual execution statistics such as examined keys and examined documents?

Indexes and Query Optimization Medium
A. db.orders.find(query).inspect("allPlans")
B. db.orders.find(query).analyze("queryPlanner")
C. db.orders.find(query).explain("executionStats")
D. db.orders.find(query).profile("executionStats")

39 A collection has an index on the array field tags. What type of index does MongoDB automatically create for that field?

Indexes and Query Optimization Medium
A. A hashed index
B. A multikey index
C. A wildcard index
D. A geospatial index

40 Only active accounts are frequently queried by email, while inactive accounts should not consume index space. Which index is most appropriate?

Indexes and Query Optimization Medium
A. db.accounts.createIndex({ email: 1 }, { sparse: false })
B. db.accounts.createIndex({ email: 1 }, { unique: false })
C. db.accounts.createIndex({ email: 1 }, { expireAfterSeconds: 0 })
D. db.accounts.createIndex({ email: 1 }, { partialFilterExpression: { active: true } })

41 An administrator runs use telemetry in mongosh, then immediately checks show dbs. The telemetry database is absent. Which explanation is correct?

Databases Hard
A. show dbs excludes every database that does not contain at least one user-defined index.
B. use creates the database only after the shell is restarted and reconnects to the same server.
C. show dbs reads a cached catalog that is refreshed only after a replica-set election.
D. use selects a database handle, but the database is materialized only after data or a collection is created.

42 A script connects with db currently set to staging and executes db.dropDatabase(). It then inserts into db.events without issuing another use command. What is the expected result?

Creating and Dropping Databases and Collections Hard
A. The insert recreates staging and implicitly creates the events collection.
B. The insert recreates only events, while staging remains absent from the catalog.
C. The insert fails because a dropped database name cannot be reused during the same connection.
D. The insert is redirected to test because the current database was dropped.

43 A collection has a JSON Schema validator and two secondary indexes. It is dropped and then recreated by inserting its first document. Which state should be expected?

Collections Hard
A. Neither the validator nor the secondary indexes return automatically after recreation.
B. Both the validator and secondary indexes return because collection metadata is retained.
C. The validator returns automatically, but the secondary indexes must be rebuilt manually.
D. The secondary indexes return automatically, but the validator must be added manually.

44 Given documents {_id: 1, x: null}, {_id: 2}, and {_id: 3, x: 0}, which query matches only the document where x explicitly has the BSON null value?

Documents Hard
A. {x: {$ne: null}}
B. {x: null}
C. {x: {$exists: false}}
D. {x: {$type: 10}}

45 A financial application must store 0.1 and perform server-side arithmetic without binary floating-point approximation. Which BSON type is most appropriate?

BSON Data Types Hard
A. string, because aggregation arithmetic converts strings without precision loss
B. Decimal128, because it represents decimal fractions with decimal semantics
C. double, because MongoDB normalizes decimal literals before storing them
D. int64, because MongoDB automatically preserves the decimal scale

46 Two clients insert the same instant using BSON Date values, one from UTC and one from a +05:30 local time converted correctly by its driver. How are these values stored?

BSON Data Types Hard
A. As different strings retaining each client's original time-zone offset
B. As the same signed millisecond value measured relative to the Unix epoch
C. As different millisecond values plus a server-side time-zone identifier
D. As the same second value with each offset stored in BSON metadata

47 Why is sorting by a default ObjectId _id only an approximation of sorting by exact creation time?

BSON Data Types Hard
A. ObjectIds are compared by their random component before their embedded timestamp.
B. ObjectIds contain a minute-level timestamp that is randomized before comparison.
C. ObjectIds contain a seconds-level timestamp, while other components determine order within a second.
D. ObjectIds store timestamps as strings whose lexical order differs from chronological order.

48 A document contains items: [{sku: "A", qty: 2}, {sku: "B", qty: 20}]. Which query requires sku: "A" and qty: {$gt: 10} to be satisfied by the same array element?

Documents Hard
A. {"items.sku": "A", "items.qty": {$gt: 10}}
B. {items: {$all: [{sku: "A"}, {qty: {$gt: 10}}]}}
C. {items: {$elemMatch: {sku: "A", qty: {$gt: 10}}}}
D. {items: {$in: [{sku: "A", qty: {$gt: 10}}]}}

49 A collection contains {_id: 7, status: "open", total: 40}. What happens when replaceOne({_id: 7}, {_id: 8, status: "closed"}) attempts to replace it?

Documents Hard
A. The replacement succeeds and moves the document from _id: 7 to _id: 8.
B. The replacement updates status while preserving both _id: 7 and total.
C. The replacement succeeds but silently preserves _id: 7 and removes total.
D. The replacement fails because the immutable _id value would be changed.

50 An order must preserve the product name and unit price exactly as they were at purchase time, even after the product catalog changes. Which model best supports this requirement?

Data Modeling in MongoDB Hard
A. Store order lines separately without product identifiers and infer products from their current prices.
B. Embed the complete order inside each product document and update every historical order on price changes.
C. Embed a snapshot of purchased product details in each order line and retain a product identifier.
D. Store only product references and resolve every name and price from the current catalog.

51 A sensor document appends one reading every second to an embedded array and is retained indefinitely. Which redesign most directly addresses the principal scaling risk?

Data Modeling in MongoDB Hard
A. Move readings into separate time-bucket documents and keep the sensor identifier on each bucket.
B. Move the sensor metadata into every reading while preserving one indefinitely growing array.
C. Keep one sensor document and add a multikey index to every field in the readings array.
D. Keep one sensor document but replace the readings array with a BSON object keyed by timestamp.

52 Users can belong to millions of groups, and groups can contain millions of users. Memberships are frequently added and removed, and queries run in both directions. Which primary model is most robust?

Data Modeling in MongoDB Hard
A. Duplicate both complete user and group documents inside every membership.
B. Create a membership collection with one document per user-group relationship.
C. Embed every group identifier in each user document and index the array.
D. Embed every user identifier in each group document and index the array.

53 A frequent query is find({tenantId: 42, createdAt: {$gte: start}}).sort({score: -1}). Which compound index most closely follows the equality-sort-range guideline?

Indexes and Query Optimization Hard
A. {createdAt: 1, tenantId: 1, score: -1}
B. {score: -1, createdAt: 1, tenantId: 1}
C. {tenantId: 1, score: -1, createdAt: 1}
D. {tenantId: 1, createdAt: 1, score: -1}

54 For an index {a: 1, b: -1}, which sort can use the index order directly when there is no filter?

Indexes and Query Optimization Hard
A. {b: -1, a: 1}
B. {a: -1, b: -1}
C. {a: -1, b: 1}
D. {a: 1, b: 1}

55 A document has both tags: ["red", "sale"] and ratings: [4, 5]. Why can creating the compound index {tags: 1, ratings: 1} fail?

Indexes and Query Optimization Hard
A. A compound index cannot index an array unless the array contains embedded documents.
B. A compound multikey index cannot index more than one array field in the same document.
C. A compound index cannot contain fields whose BSON values have different scalar types.
D. A multikey index requires every indexed array to contain the same number of elements.

56 With index {tenantId: 1, email: 1}, which query and projection can be covered by that index, assuming no relevant collation or multikey complication?

Indexes and Query Optimization Hard
A. find({tenantId: 9}, {email: 1})
B. find({tenantId: 9}, {email: 1, _id: 0})
C. find({tenantId: 9}, {email: 1, name: 1, _id: 0})
D. find({tenantId: 9}, {name: 1, _id: 0})

57 A partial index is created as {email: 1} with partialFilterExpression: {active: true}. Which query is eligible to use it without a hint?

Indexes and Query Optimization Hard
A. find({email: "a@example.com", active: true})
B. find({email: "a@example.com", active: {$ne: false}})
C. find({email: "a@example.com"})
D. find({email: "a@example.com", active: {$exists: true}})

58 An index on {name: 1} was created with a case-insensitive English collation. A query filters by name but specifies the default simple collation. What is the key optimization consequence?

Indexes and Query Optimization Hard
A. The index is generally ineligible because the query and index collations do not match.
B. The index is converted temporarily to simple collation during query planning.
C. The index supports equality matching but cannot return any projected index fields.
D. The index supports the query because collation affects sorting but never equality matching.

59 Given a standard ascending index on {username: 1}, which predicate is most likely to produce a bounded index scan?

Indexes and Query Optimization Hard
A. {username: {$regex: /^son/}}
B. {username: {$regex: /son$/}}
C. {username: {$regex: /^son/i}}
D. {username: {$regex: /son/}}

60 An explain("executionStats") result shows totalKeysExamined: 200000, totalDocsExamined: 12, and nReturned: 12. What is the strongest conclusion?

Indexes and Query Optimization Hard
A. The scan uses broad index bounds despite fetching very few documents.
B. The collection scan is efficient because most documents were rejected in memory.
C. The query is covered because only twelve documents were examined.
D. The index is selective because exactly twelve documents were returned.