Indexes Flashcards

1
Q

Which of the following statements about indexes are correct? (Select all the that apply.)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following statements about indexes are true? (Select one.)

A

b. Indexes improve query performance at the cost of write performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Single Field Index

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Compound Index

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Multikey Index

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create index

A

db.coll.createIndex({fieldName: 1 or -1}) order ascending or descending

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Single field Index unique constraint

A

{unique:true}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Get indexes

A

db.coll.getIndexes()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.)

A

d. MongoDB will return a duplicate key error, and the document will not be inserted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a multikey index? (Select one.)

A

b. An index where one of the indexed fields contains an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the maximum number of array fields per multikey index? (Select one.)

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Order of fields in compound indexes

A
  1. equality (es. active:true)
  2. sort
  3. range
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Hide index command

A

db.coll.hideIndex(index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Delete index command

A

db.coll.dropIndex(index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Drop all or multiple indexes

A

db.coll.dropIndexes([idexes])?)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A