Indexes Flashcards
Which of the following statements about indexes are correct? (Select all the that apply.)
a. Indexes are data structures that improve performance, support efficient equality matches and range-based query operations, and can return sorted results.
c. Indexes are used to make querying faster for users. One of the easiest ways to improve the performance of a slow query is create indexes on the data that is used most often.
Which of the following statements about indexes are true? (Select one.)
b. Indexes improve query performance at the cost of write performance.
Single Field Index
A single field index is an index on a single field of a document. MongoDB creates a single field index on the _id field by default, but additional indexes may be needed for other fields as well. A single field index can also be a multikey index if it operates on an array field.
Compound Index
Compound index: MongoDB supports compound indexes, where a single index structure holds references to multiple fields within a collection’s documents. A compound index is created by specifying the fields that the index should reference, followed by the order in which the fields should be sorted. The order of the fields in the index is important because it determines the order in which the documents are returned when querying the collection. A compound index can also be a multikey index if one of the fields is an array.
Multikey Index
Multikey index: A multikey index is an index on an array field. Each element in the array gets an index key, which supports efficient querying against array fields. Both single field and compound indexes can have an array field, so there are both multikey single field indexes and multikey compound indexes.
Create index
db.coll.createIndex({fieldName: 1 or -1}) order ascending or descending
Single field Index unique constraint
{unique:true}
Get indexes
db.coll.getIndexes()
You have a collection of customer details. The following is a sample document from the collection:
{
“_id”: { “$oid”: “5ca4bbcea2dd94ee58162a6a” },
“username”: “hillrachel”,
“name”: “Katherine David”,
“address”: “55711 Janet Plaza Apt. 865\nChristinachester, CT 62716”,
“birthdate”: { “$date”: { “$numberLong”: “582848134000” } },
“email”: “timothy78@hotmail.com”,
“Accounts”: [
{ “$numberInt”: “462501” },
{ “$numberInt”: “228290” },
{ “$numberInt”: “968786” },
{ “$numberInt”: “515844” },
{ “$numberInt”: “377292” }
],
“tier_and_details”: {}
}
You create a single field index on the email field, with the unique constraint set to true:
db.customers.createIndex({email:1}, {unique:true})
What would happen if you attempt to insert a new document with an email that already exists in the collection? (Select one.)
d. MongoDB will return a duplicate key error, and the document will not be inserted.
What is a multikey index? (Select one.)
b. An index where one of the indexed fields contains an array
What is the maximum number of array fields per multikey index? (Select one.)
1
Order of fields in compound indexes
- equality (es. active:true)
- sort
- range
Hide index command
db.coll.hideIndex(index)
Delete index command
db.coll.dropIndex(index)
Drop all or multiple indexes
db.coll.dropIndexes([idexes])?)