Index
Index
MongoDB Indexing
Objectives
After completing this lab, you will be able to:
Measure the time it takes to execute a query with the explain function
Describe the process of creating, listing and deleting indexes
Evaluate the effectiveness of an index
You will notice MongoDB listed there, but inactive. Which means the database is not available to use.
Once you click on it, you will see more details about it and a button to start it.
about:blank 1/7
4/30/24, 3:14 PM about:blank
Clicking on the start button will run a background process to configure and start your MongoDB server.
Shortly after that, your server is ready for use. This deployment has access control enabled and MongoDB enforces authentication. So, take note of the password as
you will need it to login as root user.
about:blank 2/7
4/30/24, 3:14 PM about:blank
This will open a new terminal at the bottom of the screen as in the image below.
about:blank 3/7
4/30/24, 3:14 PM about:blank
Run the below command on the newly opened terminal. (You can copy the code by clicking on the little copy button on the bottom right of the codeblock below and
then paste it, wherever you wish.)
1. 1
Copied! Executed!
The command contains the username and password to connect to mongodb server (the text after the -p option is the password). Your output would be different from
the one shown above. Copy the command given to you, and keep it handy. You will need it in the next step.
Or you can simply click on MongoDB CLI which does that for you.
about:blank 4/7
4/30/24, 3:14 PM about:blank
In MongoDB CLI (for example, mongo shell), switch the context to training database.
1. 1
1. use training
Copied!
1. db.createCollection("bigdata")
Copied!
1. docsToInsert = []
2. for (i = 1; i <= 200000; i++) { docsToInsert[i-1] = { "account_no": i, "balance": Math.round(Math.random() * 20000) } }
3. db.bigdata.insertMany(docsToInsert);
Copied!
Verify that 200000 documents got inserted by running the below command.
1. 1
1. db.bigdata.countDocuments()
Copied!
We will make use of the explain function to find the time taken to run the query in milliseconds.
The db.collection.explain(“executionStats”) method provides statistics about the performance of a query. These statistics can be useful in measuring if
and how a query uses an index. See db.collection.explain() for details.
about:blank 5/7
4/30/24, 3:14 PM about:blank
1. db.bigdata.find({"account_no":58982}).explain("executionStats").executionStats.executionTimeMillis
Copied!
Make a note of the milliseconds it took to run the query. We will need this at a later stage.
1. db.bigdata.createIndex({"account_no":1})
Copied!
Run the below command to get a list of indexes on the bigdata collection.
1. 1
1. db.bigdata.getIndexes()
Copied!
1. db.bigdata.find({"account_no":58982}).explain("executionStats").executionStats.executionTimeMillis
Copied!
This time, the execution time should be a lot less than previously. If you see 0, it means the query completed under 1 millisecond.
1. db.bigdata.dropIndex({"account_no":1})
Copied!
Bonus information
MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same
value for the _id field. You cannot drop this index on the _id field.
Practice exercises
1. Problem:
2. Problem:
Query for documents with a balance of 10000 and record the time taken.
3. Problem:
4. Problem:
Query for documents with a balance of 10000 and record the time taken, and compare it with the previously recorded time.
about:blank 6/7
4/30/24, 3:14 PM about:blank
Click here for Hint
Click here for Solution
Summary
In this lab, you have gained an understanding of indexing in MongoDB.
Author(s)
Muhammad Yahya
about:blank 7/7