MongoDB Indexes

MongoDB Indexes

Overview

In this unit, you will learn about indexes, how indexes support the efficient execution of queries in MongoDB, the trade-offs associated with using indexes, how to create Single Field and Compound Index, what Multikey indexes are, and how to see if queries are using indexes. Finally, you will learn how to delete an index.

Lesson 1 – Using MongoDB Indexes in Collections

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

    B. Indexes are automatically created based on usage patterns.

    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.

    D. When using an index, MongoDB reads every document in a collection to check if it matches the query that's being run.

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

    A. Indexes improve query performance and have no impact on write performance.

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

    C. Indexes have no impact on query performance but improve write performance.

    D. Indexes have a negative impact on query performance but improve write performance.

Lesson 2 – Creating a Single Field Index in MongoDB

  1. What is a single field index? (Select one.)

    A. An index that supports efficient querying against one field

    B. An index that supports efficient querying against multiple fields

    C. An index that only supports efficient querying against fields with scalar values

    D. An index that supports efficient querying against fields that are already indexed by another user-defined index

  2. 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. The new document will be inserted and replace the old document in the collection.

    B. The new document will be inserted and the old document will remain in the collection.

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

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

Lesson 3 – Creating a Multikey Index in MongoDB

  1. What is a multikey index? (Select one.)

    A. An index on one field only where the field is not an array

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

    C. An index on more than one field where none of the fields are arrays

    D. An index on more than one field where multiple fields are arrays

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

    A. 1

    B. 3

    C. 5

    D. Unlimited

Lesson 4 – Working with Compound Indexes in MongoDB

  1. What is a compound index? (Select one.)

    A. An index that supports queries that combine the field name and the value into one string

    B. An index that supports queries against unknown or arbitrary fields

    C. An index that contains references to multiple fields within a document

    D. An index that supports queries that are run on two collections at the same time

  2. What is the recommended order of fields in a compound index? (Select one.)

    A. Sort, Range, Equality

    B. Range, Sort, Equality

    C. Equality, Sort, Range

    D. The order of indexed fields is not important.

Lesson 5 – Deleting MongoDB Indexes

  1. What are the ramifications of deleting an index that is supporting a query? (Select one.)

    A. The performance of the query will improve.

    B. The performance of the query will be negatively affected.

    C. The query will fail.

    D. The query will perform as expected.

  2. You have a collection of customer details. The following is a sample document from this 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 have an index on the email field. Here’s the command you used to create the index:

     db.customers.createIndex({email:1})
    

    Before deleting it, you want to assess the impact of removing this index on the performance of the query. To do this, which command should you use? (Select one.)

    A. dropIndex()

    B. dropIndexes()

    C. getIndexes()

    D. hideIndex()