MongoDB Series - Part 4 - Indexing Basics and Types
Welcome to the fourth part of the MongoDB series - MongoDB Indexing. If you have not gone through the previous posts, then do check them -
Don't forget to Like 💜, Comment 💬 and Share 🔃 if you like the blog! 😇
First, what is Indexing?
Indexing is the process of building an Index in a database for faster query and read operations. Generally, an Index can be defined as an additional data structure the database creates for you, which stores the indexed keys (columns or fields) which normally points to the exact location of the record on Disk.
So think of it like a HashMap or a BST where you can search by the required key in O(1) or O(logn) and then directly get the address of record of Disk to read, rather than reading the whole table from the disk.
Did you know normally Databases use B Trees and B+Trees? Because they are sorted BSTs, self balanced, have nodes connected as a Linked List and N-ary as well, which decreases the depth of the tree hugely!
Type of Indexes in MongoDB
Single Field Index
The single field index is the most basic type of index in MongoDB. It involves creating an index on a single field within a collection. This type of index is suitable for queries that involve simple field filters. To create a single field index, use the `createIndex()` method with the name of the field as the argument.
# Example record
{
"_id": ObjectId("570c04a4ad233577f97dc459"),
"name": "Shrey",
"batch": "B1",
"score": 95
}
Now let's say you create the following Single Field Index -
{
score: 1 // 1 means ascending, -1 means descending
}
Now this means, you can query on this collection such as -
.find({ score: 100 }) // score == 100
.find({ score: { $gte: 80 } }) // score >= 80
Both these queries can now use the index to find the interesting records very fast.
Compound Index
A compound index involves indexing multiple fields together in a single index. This type of index is useful for queries that involve multiple fields in the filter criteria or sort operations. By creating compound indexes, you can improve the efficiency of complex queries.
Given the same above Student schema, we can create the following index -
{
batch: 1,
score: 1
}
This means that the index created uses 2 fields - batch and score in the order mentioned.
Recommended by LinkedIn
The order in Compound Indexes is very important!!
So, what queries can this index solve?
.find({ score: 100, batch: "A1" }) // batch==A1 and score==100
.find({ batch: "B2" }) // batch == B2
.find({ batch: { $in: ["A1", "A2"] }, score: { $gte: 40 } })
All the above queries can be solved using this index, but the query below will not use the index -
.find({ score: 100 }) // cannot use the compound index
This is because, the index we created has an order -- first batch then score. This means in order to query the index on score, we first need to solve batch, which is not present in the above query. Hence, index cannot be used.
Think of compound indexes as trees -- Each field is a Level, and you can only start from root node. To reach level 3, you need to cross level 1 and 2 from root.
More on order of keys, best order, performance and everything in later articles covering the ESR rule of Indexing ❤️
Multikey Index
To index a field that holds an array value, MongoDB creates an index key for each element in the array. These multikey indexes support efficient queries against array fields. Multikey indexes can be constructed over arrays that hold both scalar values (e.g. strings, numbers) and nested documents.
The above shows how MongoDB intelligently uses each singular item of an array in MongoDB to create the index, and knows that which key (the item) belongs in which all record's arrays. Too Cool..!!
You can use arrays in Compound indexes as well..!
Geospatial Indexes
Geospatial indexes are tailored for queries involving geographical data, such as location-based information. They enable efficient queries for documents within a certain proximity to a specific point on the Earth's surface.
No need to use Google APIs to find the distance between 2 Lat Long pairs. Your database can do this natively!
You can even do complex Geo queries such as finding all records within a definition on a boundary (like the boundaries of city/country) -
db.places.find({
loc: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[0, 0],
[3, 6],
[6, 1],
[0, 0],
]
]
}
}
}
});
More on Geo Queries and Indexes in later articles!
Conclusion
Diving deep into MongoDB Indexes takes time, we will go over multiple different scenarios, multiple combinations, performances, overheads, ESR rule (compound key order) and so much more in upcoming articles!
If you are interested in a more interactive, async program to learn so many tech stacks, build projects and so much more things, then register for my Backend Mentorship Program (limited seats left only, details inside)
Flutter Mobile App Developer | Cross-Platform App Specialist | Mobile Engineer
1yGreat Sir