Explore essential concepts of indexing in NoSQL databases, focusing on compound, multikey, and geospatial indexes. This quiz helps clarify how different index types work, their purposes, and scenarios where each is most effective.
Which type of index allows the database to efficiently query documents based on multiple fields, such as both 'city' and 'age' in a single search?
Explanation: A compound index indexes multiple fields in a single structure, allowing efficient queries that involve those fields together. Geospatial and hashed indexes focus on spatial data or hash values, and text indexes are optimized for text search. Only compound indexes provide efficiency for multi-field queries.
If a document in a NoSQL database has a field containing an array of values, which index type is required for efficient querying?
Explanation: A multikey index is designed to index array values so each element is indexed separately, supporting efficient queries for arrays. Single field indexes only apply to non-array fields, geospatial indexes are not suited for general arrays, and wildcards serve different purposes.
Which type of index would you use to find all places within a 5-mile radius of a given coordinate in a NoSQL database?
Explanation: Geospatial indexes efficiently support queries involving locations, distances, and coordinates. Compound indexes and text indexes do not handle spatial data, while sparse indexes are designed for handling missing data and are not specific to location searches.
In a document where 'category' is indexed before 'price' in a compound index, which query will make the best use of this index?
Explanation: Compound indexes are most effective when queries include the leading field ('category') and any subsequent indexed fields ('price'). Filtering only by the second field or an unrelated field will not utilize the index fully. Including both fields in the query is needed for optimal use.
What happens if you attempt to create a compound index on two array fields in a NoSQL collection?
Explanation: NoSQL databases generally do not allow compound indexes on multiple array fields, as this can create too many index entries and lead to performance issues. The arrays are not merged, indexed singly, or deduplicated automatically. Instead, index creation fails for this configuration.
Which index type should you use to store and query spherical geometry data, such as latitude and longitude coordinates?
Explanation: 2dsphere indexes are specifically designed for storing and querying data on a spherical surface, making them ideal for latitude and longitude. 2d indexes work for flat coordinate systems, not spheres. Text and hashed indexes are irrelevant to geometry data.
Which index type is appropriate when you want to index individual items within a nested array field, such as 'tags': [['red', 'blue'], ['green']]?
Explanation: Multikey indexes index every element in each sub-array, making them well suited for nested arrays. Single field indexes are not designed for arrays, and geohash indexes are only for location data. Sparse indexes help with null values but not array elements.
When using a geospatial index, which of the following queries is supported?
Explanation: Geospatial indexes support queries based on location, such as proximity searches within a specified distance. Sorting alphabetically, filtering by store hours, and finding highest ratings are not location-based and do not benefit from a geospatial index.
Why does the order of fields matter in a compound index such as { state: 1, city: 1 }?
Explanation: With compound indexes, queries that include the leading field take full advantage of the index structure. The order does not affect which documents are returned, prevent duplicates, or change datatypes. Field order only affects index usage efficiency.
When creating a unique compound index on 'email' and 'username', what happens if a document with an existing combination of both tries to get inserted?
Explanation: A unique compound index prevents duplicate combinations of the specified fields, so an insert with an existing combination is rejected. It does not check only one field, merge documents, or allow duplicate entries with automatic updates.