Unit 4: Indexing and Aggregation Framework - Practice Quiz

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

1 What is the main purpose of a single-field index in a NoSQL database?

Single Indexes Easy
A. To encrypt all database values
B. To speed up searches on one field
C. To combine several collections together
D. To store backup copies of documents

2 Which field could be indexed to quickly find a user by email address?

Single Indexes Easy
A. email
B. screenBrightness
C. profileColor
D. welcomeMessage

3 What is a possible disadvantage of creating many single-field indexes?

Single Indexes Easy
A. Removal of all query support
B. Guaranteed slower reads for every query
C. Automatic data deletion
D. Extra storage use

4 What does a compound index contain?

Compound Indexes Easy
A. Only one document
B. Only encrypted values
C. Multiple fields
D. A separate database server

5 A compound index is created on category and price. Which query is most directly supported by its field order?

Compound Indexes Easy
A. Find products by category
B. Find orders by shipping city
C. Find reviews by rating only
D. Find users by username

6 Why does the order of fields matter in a compound index?

Compound Indexes Easy
A. The order changes the document contents
B. Fields later in the index are automatically removed
C. Queries use the index prefix
D. Fields are always stored alphabetically

7 What is the primary purpose of a text index?

Text Indexes Easy
A. To sort numbers by size
B. To search words in text
C. To store image files
D. To calculate document totals

8 Which type of field is most suitable for a text index?

Text Indexes Easy
A. Product quantity
B. Account balance
C. Manufacturing year
D. Article description

9 A text search for database is mainly intended to find documents containing what?

Text Indexes Easy
A. A database connection object
B. The word database
C. Every document with an indexed numeric field
D. Only the number database

10 How can an appropriate index affect a read query?

Performance with Indexing Easy
A. It always increases collection size
B. It removes the need for valid query syntax
C. It converts every query into a write
D. It can reduce documents examined

11 What is a common cost of maintaining indexes during writes?

Performance with Indexing Easy
A. Automatic removal of old records
B. Conversion of text into images
C. Loss of all document fields
D. Additional update work

12 Which tool helps inspect how a database executes a query?

Performance with Indexing Easy
A. encrypt()
B. explain()
C. removeAll()
D. backupOnly()

13 What is a covered query?

Covered Queries and explain() Easy
A. A query that modifies every document
B. A query that automatically creates a text index
C. A query answered using only an index
D. A query that searches without a collection

14 Which condition is important for a query to be covered?

Covered Queries and explain() Easy
A. The collection must contain one document
B. Every field must contain an array
C. Needed fields are in the index
D. The query must use no conditions

15 What can explain() show about a query?

Covered Queries and explain() Easy
A. The user's database password
B. A permanent guarantee of future performance
C. The selected query plan
D. The physical location of every server

16 What is an aggregation pipeline?

Aggregation Pipeline Concepts Easy
A. A sequence of data-processing stages
B. A network cable between servers
C. A backup schedule for collections
D. A list of database user accounts

17 Which aggregation stage filters documents?

Aggregation Pipeline Concepts Easy
A. $limit
B. $group
C. $match
D. $sort

18 Which aggregation stage groups documents by a field?

Aggregation Pipeline Concepts Easy
A. $group
B. $match
C. $project
D. $unwind

19 Which task is a suitable real-world use of aggregation?

Real-world Aggregation Examples Easy
A. Calculate total sales by month
B. Change a user's login password
C. Compress an operating system
D. Create a network connection

20 A store wants the average rating for each product. Which operation is needed?

Real-world Aggregation Examples Easy
A. Delete reviews after reading them
B. Create one database per rating
C. Search only for product images
D. Group reviews and calculate averages

21 A MongoDB collection named orders is frequently queried using { status: "pending" }. Which index most directly supports this query?

Single Indexes Medium
A. db.orders.createIndex({ createdAt: -1 })
B. db.orders.createIndex({ customerId: 1 })
C. db.orders.createIndex({ status: 1 })
D. 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?

Single Indexes Medium
A. It can scan the index in reverse order
B. It must build a temporary text index
C. It can filter but cannot support sorting
D. It must perform a full collection scan

23 A users collection has a unique index on email. What happens when an application inserts a document whose email already exists?

Single Indexes Medium
A. MongoDB rejects the insert with an error
B. MongoDB replaces the existing user document
C. MongoDB removes uniqueness from the index
D. MongoDB stores both documents under one key

24 A collection has the compound index { customerId: 1, orderDate: -1 }. Which query can use both indexed fields most effectively?

Compound Indexes Medium
A. find({ orderDate: 42 }).sort({ customerId: -1 })
B. find({ customerId: 42 }).sort({ orderDate: -1 })
C. find({ total: 42 }).sort({ orderDate: -1 })
D. 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?

Compound Indexes Medium
A. find({ salary: 70000, name: "Mira" })
B. find({ salary: 70000 })
C. find({ department: "Sales", salary: 70000 })
D. 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?

Compound Indexes Medium
A. { amount: 1, region: 1, createdAt: -1 }
B. { createdAt: -1, amount: 1, region: 1 }
C. { region: 1, amount: 1, createdAt: -1 }
D. { 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?

Text Indexes Medium
A. find({ title: { $search: "wireless charger" } })
B. find({ description: { $text: "wireless charger" } })
C. find({ $search: { $text: "wireless charger" } })
D. 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?

Text Indexes Medium
A. { score: { $text: "textScore" } }
B. { score: { $meta: "textScore" } }
C. { score: { $sort: "textScore" } }
D. { 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?

Text Indexes Medium
A. An equality condition on category
B. A range condition on category
C. A projection excluding category
D. A descending sort on category

30 After adding many indexes, read performance improves but write latency increases. What is the most likely cause?

Performance with Indexing Medium
A. Each index disables document-level locking
B. Each write must execute every read query
C. Each write must update several indexes
D. Each index duplicates the full collection

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?

Performance with Indexing Medium
A. The predicate has low selectivity
B. The field contains a Boolean type
C. The query requires text tokenization
D. The index stores values alphabetically

32 A query examines 90,000 documents but returns only 20. Which change is most likely to improve its read efficiency?

Performance with Indexing Medium
A. Increase the number of returned fields
B. Rename the collection before querying
C. Create an index matching its filter pattern
D. Store the query inside each document

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?

Covered Queries and explain() Medium
A. find({ sku: "A1" }, { _id: 0, name: 1, stock: 1 })
B. find({ name: "A1" }, { _id: 0, sku: 1, price: 1 })
C. find({ sku: "A1" }, { _id: 1, name: 1, price: 1 })
D. 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?

Covered Queries and explain() Medium
A. The query performed a collection scan
B. The query was covered by an index
C. The query updated 120 documents
D. The query ignored all available indexes

35 In a query plan, which stage most clearly indicates that MongoDB scanned the collection rather than using an index?

Covered Queries and explain() Medium
A. IXSCAN
B. PROJECTION_COVERED
C. COLLSCAN
D. DISTINCT_SCAN

36 A pipeline must calculate total sales per region only for orders placed in 2025. Which stage order is generally most efficient?

Aggregation Pipeline Concepts Medium
A. $project, then $sort, then $match
B. $group, then $match, then $sort
C. $sort, then $group, then $match
D. $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?

Aggregation Pipeline Concepts Medium
A. $unwind
B. $lookup
C. $bucket
D. $count

38 Which accumulator correctly computes the average score for each courseId in a $group stage?

Aggregation Pipeline Concepts Medium
A. { _id: "$courseId", avgScore: { $sum: "$score" } }
B. { _id: "$score", avgScore: { $avg: "$courseId" } }
C. { _id: "$courseId", avgScore: { $avg: "$score" } }
D. { _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?

Real-world Aggregation Examples Medium
A. $sort by product, $limit to 3, $group by quantity
B. $group by product, $sort descending, $limit to 3
C. $lookup by quantity, $sort ascending, $count products
D. $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?

Real-world Aggregation Examples Medium
A. Group by quantity, then average the sale month values
B. Look up each month, then subtract quantity from price
C. Unwind saleDate, then count distinct unit prices
D. Group by sale year and month, then sum $multiply results

41 A collection has a single-field multikey index { tags: 1 }. For the query { tags: ["red", "blank"] }, which behavior is most accurate?

Single Indexes Hard
A. The index is unusable because equality comparisons cannot target array values.
B. The index finds candidates using the first array element, followed by document filtering.
C. The index matches any document containing either array element in any order.
D. The index proves the complete array equality without examining documents.

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?

Single Indexes Hard
A. It permits one null value and one additional missing value.
B. It permits only one document whose indexed key resolves to null or missing.
C. It permits unlimited null values but only one missing value.
D. It excludes both null and missing values from the unique index.

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?

Single Indexes Hard
A. { active: { $ne: false }, email: "a@example.com" }
B. { email: "a@example.com" }
C. { active: false, email: "a@example.com" }
D. { 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?

Compound Indexes Hard
A. { tenantId: 1, createdAt: 1, rating: -1 }
B. { tenantId: 1, rating: -1, createdAt: 1 }
C. { rating: -1, createdAt: 1, tenantId: 1 }
D. { 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?

Compound Indexes Hard
A. { a: -1, b: 1, c: -1 }
B. { a: -1, b: 1, c: 1 }
C. { a: 1, b: 1, c: -1 }
D. { 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?

Compound Indexes Hard
A. Both fields may be arrays if each array contains only scalar values.
B. Only the first field may ever contain an array in the collection.
C. Both fields may be arrays if their arrays always have equal lengths.
D. No indexed document may contain arrays in both fields simultaneously.

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?

Text Indexes Hard
A. An equality predicate on tenantId together with the $text predicate
B. A descending sort on tenantId together with the $text predicate
C. A range predicate on tenantId together with the $text predicate
D. A projection of tenantId together with the $text predicate

48 Which query correctly returns documents matching database indexing and orders them by MongoDB text relevance?

Text Indexes Hard
A. find({ $text: { $search: "database indexing" } }, { score: { $meta: "textScore" } }).sort({ score: { $meta: "textScore" } })
B. find({ $text: { $search: "database indexing" } }).sort({ score: -1 })
C. find({ body: { $text: "database indexing" } }).sort({ $textScore: -1 })
D. 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?

Text Indexes Hard
A. Set language_override to lang when creating the index.
B. Set default_language to the literal field path $lang.
C. Set textScore to lang in every text-search query.
D. Add { 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?

Performance with Indexing Hard
A. Lower memory use because indexes compress the underlying documents
B. Higher replication speed because secondaries replay indexed writes directly
C. Higher write amplification, storage use, and cache pressure
D. Lower insertion cost because index pages reduce collection scans

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?

Performance with Indexing Hard
A. Single-field indexes are ignored when a query returns over one million documents.
B. The index has low selectivity, so scanning keys and fetching many documents may cost more.
C. MongoDB requires boolean indexes to be declared sparse before they are usable.
D. Boolean fields are never eligible for B-tree index scans in MongoDB.

52 Given the index { tenantId: 1, status: 1, total: 1 }, which query and projection can be fully covered by that index?

Covered Queries and explain() Hard
A. find({ tenantId: 4, status: "paid" }, { total: 1, _id: 0 })
B. find({ tenantId: 4 }, { customerName: 1, _id: 0 })
C. find({ status: "paid" }, { total: 1, _id: 1 })
D. 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?

Covered Queries and explain() Hard
A. find({ event: "sale" }, { tags: 1, _id: 0 })
B. find({ tags: { $elemMatch: { $eq: "vip" } } }, { event: 1, _id: 0 })
C. find({ event: "sale" }, { event: 1, _id: 0 })
D. 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?

Covered Queries and explain() Hard
A. { createdAt: -1 }
B. { tenant: 1, status: 1, createdAt: -1 }
C. { status: 1, createdAt: 1, tenant: -1 }
D. { 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?

Aggregation Pipeline Concepts Hard
A. Duplicate the complete $match before and after $addFields to preserve computed values.
B. Move only the discounted predicate before $addFields and retain region afterward.
C. Move the entire $match before $addFields because match stages always execute first.
D. Move the 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?

Aggregation Pipeline Concepts Hard
A. Six output documents
B. Seven output documents
C. Four output documents
D. Five output documents

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?

Aggregation Pipeline Concepts Hard
A. Whenever both local operands are field paths, including missing and array values
B. Only when the foreign collection is unsharded and the local collection has the same index
C. When each local variable resolves to a scalar constant for the foreign subquery and the index is not multikey
D. Only when $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?

Real-world Aggregation Examples Hard
A. $match paid orders, $unwind line items, then group by product and sum quantity times price
B. $match paid orders, then sum each order total once for every product
C. $group orders by product, $unwind grouped items, then average quantity and price
D. $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?

Real-world Aggregation Examples Hard
A. Sort by deviceId ascending and timestamp descending, then group by device using $first
B. Group by device using $max on timestamp, which automatically retains the complete document
C. Sort only by timestamp ascending, then group by device using $first
D. Group by device using $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?

Real-world Aggregation Examples Hard
A. Run $skip and $limit before $match, then use $group to estimate the total.
B. Apply common filtering, then use $facet with a paginated data branch and a separate count branch.
C. Apply $limit before $facet, then count the limited documents in a metadata branch.
D. Apply $count before $facet, then reconstruct paginated documents from the count.