Sitemap
Ace D Baugh

Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer

Indexes

2 min readNov 23, 2022
Press enter or click to view image in full size
Photo by Mick Haupt on Unsplash

If you were a computer and had been given the command to search for a specific word in the dictionary, you would start at the very beginning and go page by page looking for that word. You would have to read each word and definition. We know that if we were looking for “Happy”, we could skip letters a through g because happy won’t be found there. We don’t even have to read the definition of the words found in the H section. Much like the index of a book, database indexes are a way for a computer to find things quicker and more efficiently. MongoDB uses indexes much like any other database management system. It will index things you think will be searched most frequently. For example, if searching for cars, you would want to index by make and model, not by year or car color. An index holds information or an identifier of the database that makes sense to search for and then links to the original document. The following example is how you would create a compound index for both make and model.

db.cars.createIndex({“make”: 1, “model”: 1})

Get Ace D Baugh’s stories in your inbox

Join Medium for free to get updates from this writer.

Documents in your database will have many different fields and some might have many options for what is stored there like the car could be built by many different makers, but the number of wheels the car has may only have two options, that of 3 or 4 wheels (3-wheeled cars are cool). When a field has many options, it is considered high in cardinality. Cardinality is based on distinct values and is considered a good way to index collections.

In my church, there are twelve apostles who assist in running the church and give counsel to the members. There are only twelve and when one dies, they are replaced with another. One could write this in a collection called “current apostles” and there would only be 12 documents. This is what is known as a capped collection. Capped collections are usually preformatted as they have a structure that will not change, although the data inside may be updated.

Sparse indexes only contain those entries that contain the field in question. So if a username is created but not needed when someone registers to the site with an email address, it isn’t indexed.

References

CHODOROW, KRISTINA (2017). Mongodb: The definitive guide. O’REILLY MEDIA, INC, USA.

--

--

Ace D Baugh
Ace D Baugh

Written by Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer