Consider a MongoDB collection users with a single field index on age. Which query will most likely use this index to improve performance?
db.users.createIndex({ age: 1 })
db.users.find({ age: { $gt: 30 } })Indexes help queries that filter on the indexed field.
The index on age helps queries filtering by age. Query C filters by age and uses the index. Query B filters by name, which is not indexed. Query A uses $exists which does not efficiently use the index. Query D filters by age and name, but only age is indexed, so the index helps only partially.
What is true about a single field index created with { field: 1 } in MongoDB?
Think about default behavior of indexes in MongoDB.
By default, single field indexes are non-unique unless specified. They speed up queries filtering by the indexed field. They do not index all fields or only documents with the field.
Which of the following commands correctly creates a single field ascending index on the email field in MongoDB?
Remember the syntax for specifying fields in an object.
Option D uses the correct syntax: an object with field name as key and 1 for ascending order. Option D is missing braces. Option D uses invalid arrow syntax. Option D uses an array which is invalid.
You have a collection with fields category and price. You often run queries filtering only by category. Which index is best to optimize these queries?
Indexes should match the fields used in query filters.
Since queries filter only by category, a single field index on category is best. Compound indexes help when filtering by multiple fields in order. Index on price alone won't help filtering by category. No index means slower queries.
A MongoDB collection has a single field index on status. A query db.orders.find({ status: { $in: ['shipped', 'delivered'] } }) is slow. What is the most likely reason?
Think about index usage and maintenance in MongoDB.
The $in operator can use single field indexes. The index is likely used but query is slow due to outdated statistics or fragmentation. Large collections may need compound indexes but that is not the main cause here.