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 combine several collections together
C. To store backup copies of documents
D. To speed up searches on one field

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

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

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

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

4 What does a compound index contain?

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

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 reviews by rating only
B. Find users by username
C. Find orders by shipping city
D. Find products by category

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

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

7 What is the primary purpose of a text index?

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

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

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

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

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

10 How can an appropriate index affect a read query?

Performance with Indexing Easy
A. It removes the need for valid query syntax
B. It always increases collection size
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. Loss of all document fields
B. Conversion of text into images
C. Additional update work
D. Automatic removal of old records

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

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

13 What is a covered query?

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

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

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

15 What can explain() show about a query?

Covered Queries and explain() Easy
A. The user's database password
B. The selected query plan
C. A permanent guarantee of future performance
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. $match
B. $group
C. $limit
D. $sort

18 Which aggregation stage groups documents by a field?

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

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

Real-world Aggregation Examples Easy
A. Change a user's login password
B. Calculate total sales by month
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. Group reviews and calculate averages
B. Search only for product images
C. Create one database per rating
D. Delete reviews after reading them

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({ status: "text" })
B. db.orders.createIndex({ createdAt: -1 })
C. db.orders.createIndex({ customerId: 1 })
D. db.orders.createIndex({ status: 1 })

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 must perform a full collection scan
B. It can scan the index in reverse order
C. It must build a temporary text index
D. It can filter but cannot support sorting

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 replaces the existing user document
B. MongoDB removes uniqueness from the index
C. MongoDB stores both documents under one key
D. MongoDB rejects the insert with an error

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({ orderDate: { $gt: 42 } })
C. find({ customerId: 42 }).sort({ orderDate: -1 })
D. find({ total: 42 }).sort({ orderDate: -1 })

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({ name: "Mira" })
B. find({ department: "Sales", salary: 70000 })
C. find({ salary: 70000 })
D. find({ salary: 70000, 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. { region: 1, createdAt: -1, amount: 1 }
B. { amount: 1, region: 1, createdAt: -1 }
C. { createdAt: -1, amount: 1, region: 1 }
D. { region: 1, amount: 1, createdAt: -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({ $text: { $search: "wireless charger" } })
B. find({ title: { $search: "wireless charger" } })
C. find({ $search: { $text: "wireless charger" } })
D. find({ description: { $text: "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: { $sort: "textScore" } }
B. { score: { $meta: "textScore" } }
C. { score: { $text: "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. A projection excluding category
B. An equality condition on category
C. A range condition on 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 write must execute every read query
B. Each write must update several indexes
C. Each index duplicates the full collection
D. Each index disables document-level locking

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 query requires text tokenization
B. The index stores values alphabetically
C. The predicate has low selectivity
D. The field contains a Boolean type

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. Store the query inside each document
B. Create an index matching its filter pattern
C. Rename the collection before querying
D. Increase the number of returned fields

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: 1, name: 1, price: 1 })
B. find({ sku: "A1" }, { _id: 0, sku: 1, price: 1 })
C. find({ name: "A1" }, { _id: 0, sku: 1, price: 1 })
D. find({ sku: "A1" }, { _id: 0, name: 1, stock: 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 updated 120 documents
B. The query ignored all available indexes
C. The query was covered by an index
D. The query performed a collection scan

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. PROJECTION_COVERED
B. IXSCAN
C. DISTINCT_SCAN
D. COLLSCAN

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. $sort, then $group, then $match
B. $group, then $match, then $sort
C. $project, then $sort, 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. $bucket
B. $lookup
C. $unwind
D. $count

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

Aggregation Pipeline Concepts Medium
A. { _id: "$score", avgScore: { $avg: "$courseId" } }
B. { _id: "$courseId", avgScore: { $sum: "$score" } }
C. { _id: "$courseId", avgScore: { $divide: "$score" } }
D. { _id: "$courseId", avgScore: { $avg: "$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. $lookup by quantity, $sort ascending, $count products
B. $sort by product, $limit to 3, $group by quantity
C. $group by product, $sort descending, $limit to 3
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. Unwind saleDate, then count distinct unit prices
B. Group by quantity, then average the sale month values
C. Group by sale year and month, then sum $multiply results
D. Look up each month, then subtract quantity from price

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 proves the complete array equality without examining documents.
B. The index matches any document containing either array element in any order.
C. The index is unusable because equality comparisons cannot target array values.
D. The index finds candidates using the first array element, followed by document filtering.

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 unlimited null values but only one missing value.
B. It permits one null value and one additional missing value.
C. It excludes both null and missing values from the unique index.
D. It permits only one document whose indexed key resolves to null or missing.

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: true, email: "a@example.com" }
B. { email: "a@example.com" }
C. { active: { $ne: false }, email: "a@example.com" }
D. { active: false, 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. { rating: -1, createdAt: 1, tenantId: 1 }
B. { tenantId: 1, createdAt: 1, rating: -1 }
C. { tenantId: 1, rating: -1, createdAt: 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. No indexed document may contain arrays in both fields simultaneously.
B. Both fields may be arrays if their arrays always have equal lengths.
C. Only the first field may ever contain an array in the collection.
D. Both fields may be arrays if each array contains only scalar values.

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. A descending sort on tenantId together with the $text predicate
B. An equality predicate 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({ body: { $text: "database indexing" } }).sort({ $textScore: -1 })
C. find({ $text: { $search: "database indexing" } }).sort({ score: -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. Add { lang: "text" } as the first field of the index.
C. Set textScore to lang in every text-search query.
D. Set default_language to the literal field path $lang.

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 insertion cost because index pages reduce collection scans
B. Lower memory use because indexes compress the underlying documents
C. Higher write amplification, storage use, and cache pressure
D. Higher replication speed because secondaries replay indexed writes directly

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. Boolean fields are never eligible for B-tree index scans in MongoDB.
D. MongoDB requires boolean indexes to be declared sparse before they are usable.

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

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" }, { event: 1, _id: 0 })
B. find({ event: "sale" }, { event: 1, tags: 1, _id: 0 })
C. find({ event: "sale" }, { tags: 1, _id: 0 })
D. find({ tags: { $elemMatch: { $eq: "vip" } } }, { event: 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. Move only the discounted predicate before $addFields and retain region afterward.
B. Move the region predicate before $addFields while leaving the discounted predicate after it.
C. Move the entire $match before $addFields because match stages always execute first.
D. Duplicate the complete $match before and after $addFields to preserve computed values.

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. Four output documents
B. Five output documents
C. Seven output documents
D. Six 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. Only when $expr is replaced by $where and both comparisons use JavaScript equality
B. Only when the foreign collection is unsharded and the local collection has the same index
C. Whenever both local operands are field paths, including missing and array values
D. When each local variable resolves to a scalar constant for the foreign subquery and the index is not multikey

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. $unwind line items, $match paid orders, then count documents by product
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. $match paid orders, $unwind line items, then group by product and sum quantity times price

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. Group by device using $max on timestamp, which automatically retains the complete document
B. Sort by deviceId ascending and timestamp descending, then group by device using $first
C. Group by device using $last, then sort the grouped results by timestamp descending
D. Sort only by timestamp ascending, then group by device using $first

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 $count before $facet, then reconstruct paginated documents from the count.
D. Apply $limit before $facet, then count the limited documents in a metadata branch.