Labs ICT
โญ Pro Login

Single Field Indexes

Indexing on a single field.

Single Field Indexes

A single field index is exactly what it sounds like โ€” an index on one field. It's the simplest and most common type of index you'll create in MongoDB. Think of it like creating a phone book sorted by last name. You look up the name, find it in the sorted list, and get the associated number.

Every index you create starts with a basic command: createIndex(). The value you pass is either 1 for ascending order or -1 for descending order. Don't overthink this โ€” MongoDB can traverse B-trees in either direction efficiently.

Creating Single Field Indexes

Let's say you have a users collection and you frequently search for users by their email. Without an index, MongoDB scans every document. With an index, it jumps straight to the match.

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

You can also name your indexes explicitly. By default, MongoDB generates names like email_1, but custom names make your life easier when debugging index issues later.

db.users.createIndex({ email: 1 }, { name: "email_lookup_index" })

Trust me, when you have dozens of indexes across collections, you'll thank yourself for giving them meaningful names. It's like labeling your spice jars instead of putting them all in unmarked containers.

Sort Order

So does it matter whether you use 1 or -1? For single field indexes, not really. MongoDB can read the B-tree in either direction with equal performance. Whether your index is ascending or descending won't change how fast your queries run.

However, the sort order matters when you're doing compound indexes โ€” but that's a topic for the next lesson. For now, just know that for a single field, pick whichever order makes intuitive sense for your use case. If you typically sort results newest first, use -1. If oldest first, use 1.

db.logs.find({ level: "error" }).sort({ timestamp: -1 })

An index on { timestamp: -1 } would be a natural fit here since you're sorting in descending order anyway.

Performance Impact

The performance difference between indexed and non-indexed queries is dramatic. We're talking from seconds to milliseconds. On a collection with 1 million documents, a query without an index might take 500ms or more. With the right index, the same query completes in under 1ms.

But here's the trade-off โ€” indexes aren't free. Every index you create adds overhead to write operations (inserts, updates, deletes) because MongoDB has to update the index structure alongside the document. It also uses disk space and memory.

db.products.find({ category: "electronics", price: { $lt: 100 } })

The sweet spot is indexing fields that are read frequently but written infrequently. Your email field gets queried on every login but rarely changes โ€” that's a perfect candidate. A field that changes on every write but is rarely queried? Probably skip the index.

You can check your current indexes anytime with db.collection.getIndexes(). Use this to audit what you have and clean up anything that isn't pulling its weight.

๐Ÿงช Quick Quiz

What command creates an index?