Deep dive into Covered Queries in MongoDB and Analyzing queries

Last updated on May 30 2022
Satyen Sahu

Table of Contents

Deep dive into Covered Queries in MongoDB and Analyzing queries

MongoDB – Covered Queries

In this blog, we’ll learn about covered queries.

What is a Covered Question?

As per the official MongoDB documentation, a covered question is a question within which −
• All the fields within the question are part of an withindex.
• All the fields returned within the question are within the same index.
Since all the fields present within the question are part of an index, MongoDB matches the question conditions and returns the result using the same index without actually looking inside the documents. Since indexes are present in RAM, fetching data from indexes is much faster as compared to fetching data by scanning documents.

Using Covered Queries

To test covered queries, consider the following document in the users collection −
{
“_id”: ObjectId(“53402597d852426020000002”),
“contact”: “987654321”,
“dob”: “01-01-1991”,
“gender”: “M”,
“name”: “Tom Benzamin”,
“user_name”: “tombenzamin”
}
We’ll first create a compound index for the users collection on the fields gender and user_name using the following question −
>db.users.ensureIndex({gender:1,user_name:1})
Now, this index will cover the following question −
>db.users.find({gender:”M”},{user_name:1,_id:0})
That is to say that for the above question, MongoDB would not go looking into database documents. Instead it would fetch the required data from indexed data which is very fast.
Since our index does not include _id field, we have explicitly excluded it from result set of our question, as MongoDB by default returns _id field in every question. So the following question would not have been covered inside the index created above −
>db.users.find({gender:”M”},{user_name:1})
Lastly, remember that an index cannot cover a question if −
• Any of the indexed fields is an array
• Any of the indexed fields is a subdocument

MongoDB – Analyzing Queries

Analyzing queries is a very important aspect of measuring how effective the database and indexing design is. We’ll learn about the frequently used $explain and $hint queries.

Using $explain

The $explain operator provides information on the question, indexes used in a question and other statistics. It is very useful when analyzing how well your indexes are optimized.
In the last chapter, we had already created an index for the users collection on fields gender and user_name using the subsequent question −
>db.users.ensureIndex({gender:1,user_name:1})
We’ll now use $explain on the subsequent question −
>db.users.find({gender:”M”},{user_name:1,_id:0}).explain()
The above explain() question returns the subsequent analyzed result −
{
“cursor” : “BtreeCursor gender_1_user_name_1”,
“isMultiKey” : false,
“n” : 1,
“nscannedObjects” : 0,
“nscanned” : 1,
“nscannedObjectsAllPlans” : 0,
“nscannedAllPlans” : 1,
“scanAndOrder” : false,
“indexOnly” : true,
“nYields” : 0,
“nChunkSkips” : 0,
“millis” : 0,
“indexBounds” : {
“gender” : [
[
“M”,
“M”
]
],
“user_name” : [
[
{
“$minElement” : 1
},
{
“$maxElement” : 1
}
]
]
}
}
We’ll now check out the fields during this result set −
• The true value of indexOnly indicates that this question has used indexing.
• The cursor field specifies the sort of cursor used. BTreeCursor sort indicates that an index was used and also gives the name of the index used. BasicCursor indicates that a full scan was made without using any indexes.
• n indicates the number of documents matching returned.
• nscannedObjects indicates the total number of documents scanned.
• nscanned indicates the total number of documents or index entries scanned.
Using $hint
The $hint operator forces the question optimizer to use the specified index to run a question. This is particularly useful when you want to test performance of a question with different indexes. For example, the subsequent question specifies the index on fields gender and user_name to be used for this question −
>db.users.find({gender:”M”},{user_name:1,_id:0}).hint({gender:1,user_name:1})
To analyze the above question using $explain −
>db.users.find({gender:”M”},{user_name:1,_id:0}).hint({gender:1,user_name:1}).explain()

So, this brings us to the end of blog. This Tecklearn ‘Deep dive into Covered Queries in MongoDB and Analyzing queries’ helps you with commonly asked questions if you are looking out for a job in MongoDB and No-SQL Database Domain.
If you wish to learn and build a career in MongoDB or No-SQL Database domain, then check out our interactive, MongoDB Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

MongoDB Training

MongoDB Training

About the Course

Tecklearn’s MongoDB Training helps you to master the NoSQL database. The course makes you job-ready by letting you comprehend schema design, data modelling, replication, and query with MongoDB through real-time examples. Along with this, you’ll also gain hands-on expertise in installing, configuring, and maintaining the MongoDB environment, including monitoring and operational strategies from this online MongoDB training. Upon completion of this online training, you will hold a solid understanding and hands-on experience with MongoDB.

Why Should you take MongoDB Training?

• MongoDB – a $36 billion to a $40 billion market growing at 8% to 9% annually – Forbes.com
• Average salary of a Mongo DB certified professional is $134k – Indeed.com
• MongoDB has more than 900 customers, including 27 Fortune 100 companies like Cisco, eBay, eHarmony, MetLife & Salesforce.com

What you will Learn in this Course?

Introduction to MongoDB and Importance of NoSQL
• Understanding the basic concepts of RDBMS
• What is NoSQL Database and its significance?
• Challenges of RDBMS and How NoSQL suits Big Data needs
• Types of NoSQL Database and NoSQL vs. SQL Comparison
• CAP Theorem and Implementing NoSQL
• Introduction to MongoDB and its advantages
• Design Goals for MongoDB Server and Database, MongoDB tools
• Collection, Documents and Key Value Pair
• Introduction to JSON and BSON documents
• MongoDB installation
MongoDB Installation
• MongoDB Installation
• Basic MongoDB commands and operations,
• Mongo Chef (MongoGUI) Installation
Schema Design and Data Modelling
• Why Data Modelling?
• Data Modelling Approach
• Data Modelling Concepts
• Difference between MongoDB and RDBMS modelling
• Challenges for Data Modelling
• Model Relationships between Documents
• Data Model Examples and Patterns
• Model Tree Structures
CRUD Operations
• MongoDB Architecture
• CRUD Introduction and MongoDB CRUD Concepts
• MongoDB CRUD Concerns (Read and Write Operations)
• Cursor Query Optimizations and Query Behaviour in MongoDB
• Distributed Read and Write Queries
• MongoDB Datatypes
Indexing and Aggregation Framework
• Concepts of Data aggregation and types and data indexing concepts
• Introduction to Aggregation
• Approach to Aggregation
• Types of Aggregation: Pipeline, MapReduce and Single Purpose
• Performance Tuning
MongoDB Administration
• Administration concepts in MongoDB
• MongoDB Administration activities: Health check, recovery, backup, database sharing and profiling, performance tuning etc.
• Backup and Recovery Methods for MongoDB
• Export and Import of Data from MongoDB
• Run time configuration of MongoDB
MongoDB Security
• Security Introduction
• MongoDB security Concepts and security approach
• MongoDB integration with Java and Robomongo
Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Deep dive into Covered Queries in MongoDB and Analyzing queries"

Leave a Message

Your email address will not be published. Required fields are marked *