Unit 4: Indexing and Aggregation Framework - Practice Quiz
1 What is the main purpose of a single-field index in a NoSQL database?
2 Which field could be indexed to quickly find a user by email address?
3 What is a possible disadvantage of creating many single-field indexes?
4 What does a compound index contain?
5
A compound index is created on category and price. Which query is most directly supported by its field order?
6 Why does the order of fields matter in a compound index?
7 What is the primary purpose of a text index?
8 Which type of field is most suitable for a text index?
9
A text search for database is mainly intended to find documents containing what?
10 How can an appropriate index affect a read query?
11 What is a common cost of maintaining indexes during writes?
12 Which tool helps inspect how a database executes a query?
encrypt()
explain()
removeAll()
backupOnly()
13 What is a covered query?
14 Which condition is important for a query to be covered?
15
What can explain() show about a query?
16 What is an aggregation pipeline?
17 Which aggregation stage filters documents?
$limit
$group
$match
$sort
18 Which aggregation stage groups documents by a field?
$group
$match
$project
$unwind
19 Which task is a suitable real-world use of aggregation?
20 A store wants the average rating for each product. Which operation is needed?
21
A MongoDB collection named orders is frequently queried using { status: "pending" }. Which index most directly supports this query?
db.orders.createIndex({ createdAt: -1 })
db.orders.createIndex({ customerId: 1 })
db.orders.createIndex({ status: 1 })
db.orders.createIndex({ status: "text" })
22
A query filters products with { price: { $gte: 500 } } and sorts them using { price: -1 }. The collection has an index { price: 1 }. How can MongoDB use this index?
23
A users collection has a unique index on email. What happens when an application inserts a document whose email already exists?
24
A collection has the compound index { customerId: 1, orderDate: -1 }. Which query can use both indexed fields most effectively?
find({ orderDate: 42 }).sort({ customerId: -1 })
find({ customerId: 42 }).sort({ orderDate: -1 })
find({ total: 42 }).sort({ orderDate: -1 })
find({ orderDate: { $gt: 42 } })
25
A collection has an index { department: 1, salary: -1, name: 1 }. Which query relies only on a valid prefix of this index?
find({ salary: 70000, name: "Mira" })
find({ salary: 70000 })
find({ department: "Sales", salary: 70000 })
find({ name: "Mira" })
26
An application commonly runs { region: "West", amount: { $gt: 1000 } } and sorts by { createdAt: -1 }. Which index best follows the equality-sort-range guideline?
{ amount: 1, region: 1, createdAt: -1 }
{ createdAt: -1, amount: 1, region: 1 }
{ region: 1, amount: 1, createdAt: -1 }
{ region: 1, createdAt: -1, amount: 1 }
27
A collection has a text index on title and description. Which query searches both indexed fields for the terms wireless and charger?
find({ title: { $search: "wireless charger" } })
find({ description: { $text: "wireless charger" } })
find({ $search: { $text: "wireless charger" } })
find({ $text: { $search: "wireless charger" } })
28 A text search returns several articles. Which projection adds each document's relevance score so the results can be sorted by relevance?
{ score: { $text: "textScore" } }
{ score: { $meta: "textScore" } }
{ score: { $sort: "textScore" } }
{ score: { $meta: "indexScore" } }
29
A compound text index is defined as { category: 1, content: "text" }. What must a query include to use the text portion of this index?
category
category
category
category
30 After adding many indexes, read performance improves but write latency increases. What is the most likely cause?
31
A query uses an indexed active field, but almost every document has active: true. Why might the index provide little performance benefit for that value?
32 A query examines 90,000 documents but returns only 20. Which change is most likely to improve its read efficiency?
33
A collection has an index { sku: 1, price: 1 }. Which query can be covered by this index, assuming sku and price are scalar fields?
find({ sku: "A1" }, { _id: 0, name: 1, stock: 1 })
find({ name: "A1" }, { _id: 0, sku: 1, price: 1 })
find({ sku: "A1" }, { _id: 1, name: 1, price: 1 })
find({ sku: "A1" }, { _id: 0, sku: 1, price: 1 })
34
An explain("executionStats") result shows totalKeysExamined: 120, totalDocsExamined: 0, and nReturned: 120. What does this most strongly indicate?
35 In a query plan, which stage most clearly indicates that MongoDB scanned the collection rather than using an index?
IXSCAN
PROJECTION_COVERED
COLLSCAN
DISTINCT_SCAN
36 A pipeline must calculate total sales per region only for orders placed in 2025. Which stage order is generally most efficient?
$project, then $sort, then $match
$group, then $match, then $sort
$sort, then $group, then $match
$match, then $group, then $sort
37
Documents contain an items array, and a report needs one pipeline document per array element. Which stage should be used?
$unwind
$lookup
$bucket
$count
38
Which accumulator correctly computes the average score for each courseId in a $group stage?
{ _id: "$courseId", avgScore: { $sum: "$score" } }
{ _id: "$score", avgScore: { $avg: "$courseId" } }
{ _id: "$courseId", avgScore: { $avg: "$score" } }
{ _id: "$courseId", avgScore: { $divide: "$score" } }
39
An online store needs the three products with the highest total quantity sold. After unwinding items, which sequence should follow?
$sort by product, $limit to 3, $group by quantity
$group by product, $sort descending, $limit to 3
$lookup by quantity, $sort ascending, $count products
$limit to 3, $group by product, $sort ascending
40
A sales dashboard needs monthly revenue from documents containing saleDate, quantity, and unitPrice. Which approach is appropriate?
saleDate, then count distinct unit prices
$multiply results
41
A collection has a single-field multikey index { tags: 1 }. For the query { tags: ["red", "blank"] }, which behavior is most accurate?
42
A unique single-field index is created on email. Some documents have email: null, while others omit email. What constraint does the index impose?
43
An index is created as { email: 1 } with partialFilterExpression: { active: true }. Which query is eligible to use the complete partial index without risking omitted matching documents?
{ active: { $ne: false }, email: "a@example.com" }
{ email: "a@example.com" }
{ active: false, email: "a@example.com" }
{ active: true, email: "a@example.com" }
44
A query filters by tenantId = 7, restricts createdAt to a range, and sorts by rating descending. Which compound index best follows the ESR guideline while avoiding a blocking sort?
{ tenantId: 1, createdAt: 1, rating: -1 }
{ tenantId: 1, rating: -1, createdAt: 1 }
{ rating: -1, createdAt: 1, tenantId: 1 }
{ createdAt: 1, tenantId: 1, rating: -1 }
45
Given the compound index { a: 1, b: -1, c: 1 } and no filtering predicate, which sort can be satisfied by scanning the entire index in reverse?
{ a: -1, b: 1, c: -1 }
{ a: -1, b: 1, c: 1 }
{ a: 1, b: 1, c: -1 }
{ a: -1, b: -1, c: -1 }
46
A compound index { tags: 1, ratings: 1 } is required, and both fields may contain arrays. Under which condition can MongoDB maintain this compound multikey index?
47
A compound text index is defined as { tenantId: 1, body: "text" }. Which predicate is required for efficient use of this index in a text search?
tenantId together with the $text predicate
tenantId together with the $text predicate
tenantId together with the $text predicate
tenantId together with the $text predicate
48
Which query correctly returns documents matching database indexing and orders them by MongoDB text relevance?
find({ $text: { $search: "database indexing" } }, { score: { $meta: "textScore" } }).sort({ score: { $meta: "textScore" } })
find({ $text: { $search: "database indexing" } }).sort({ score: -1 })
find({ body: { $text: "database indexing" } }).sort({ $textScore: -1 })
find({ $text: { $search: "database indexing" } }).sort({ relevance: "textScore" })
49
Documents contain a lang field that identifies the language used by each document's indexed text. Which text-index configuration enables per-document stemming and stop-word rules?
language_override to lang when creating the index.
default_language to the literal field path $lang.
textScore to lang in every text-search query.
{ lang: "text" } as the first field of the index.
50 A write-heavy collection has ten overlapping indexes, while read latency is already acceptable. What is the most likely effect of adding another index?
51
A collection has an index { active: 1 }, but { active: true } matches approximately 48% of ten million documents. Why might the optimizer prefer a collection scan?
52
Given the index { tenantId: 1, status: 1, total: 1 }, which query and projection can be fully covered by that index?
find({ tenantId: 4, status: "paid" }, { total: 1, _id: 0 })
find({ tenantId: 4 }, { customerName: 1, _id: 0 })
find({ status: "paid" }, { total: 1, _id: 1 })
find({ tenantId: 4 }, { total: 1, customerName: 1, _id: 0 })
53
The index { event: 1, tags: 1 } is multikey because tags is an array. Which query can still be covered, assuming no other stage forces a fetch?
find({ event: "sale" }, { tags: 1, _id: 0 })
find({ tags: { $elemMatch: { $eq: "vip" } } }, { event: 1, _id: 0 })
find({ event: "sale" }, { event: 1, _id: 0 })
find({ event: "sale" }, { event: 1, tags: 1, _id: 0 })
54
For a query filtering { tenant: 42, status: "open" }, sorting by { createdAt: -1 }, and limiting to 20, explain("executionStats") shows an IXSCAN on { status: 1 }, totalKeysExamined: 500000, totalDocsExamined: 500000, and nReturned: 20. Which index is the strongest improvement?
{ createdAt: -1 }
{ tenant: 1, status: 1, createdAt: -1 }
{ status: 1, createdAt: 1, tenant: -1 }
{ status: 1, tenant: 1 }
55
Consider a pipeline where $addFields computes discounted, followed by $match: { region: "EU", discounted: { $lt: 100 } }. Which optimization is semantically valid?
$match before and after $addFields to preserve computed values.
discounted predicate before $addFields and retain region afterward.
$match before $addFields because match stages always execute first.
region predicate before $addFields while leaving the discounted predicate after it.
56
Four documents have items values ['a','b'], [], missing, and 'x', respectively. How many output documents result from $unwind with preserveNullAndEmptyArrays: true?
57
A correlated $lookup compares foreign stock_item with a local item and foreign instock with local ordered using $expr. When can a foreign compound index { stock_item: 1, instock: 1 } support both comparisons?
$expr is replaced by $where and both comparisons use JavaScript equality
58
Each paid order contains lineItems, where every item has productId, quantity, and unitPrice. Which stage sequence correctly calculates revenue per product?
$match paid orders, $unwind line items, then group by product and sum quantity times price
$match paid orders, then sum each order total once for every product
$group orders by product, $unwind grouped items, then average quantity and price
$unwind line items, $match paid orders, then count documents by product
59
A telemetry collection stores multiple status records per device. Which pipeline pattern reliably returns the most recent complete record for every deviceId?
deviceId ascending and timestamp descending, then group by device using $first
$max on timestamp, which automatically retains the complete document
$first
$last, then sort the grouped results by timestamp descending
60 An API must return one page of filtered results and the total number of matching documents from the same aggregation input. Which pipeline design is most appropriate?
$skip and $limit before $match, then use $group to estimate the total.
$facet with a paginated data branch and a separate count branch.
$limit before $facet, then count the limited documents in a metadata branch.
$count before $facet, then reconstruct paginated documents from the count.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →