Wildcard Indexes
Wildcard Indexes: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 47 of 100
Wildcard Indexes
Foundations & CRUD ✓ → Queries & Schema → Aggregation & Scale → Atlas & Projects
Queries & Schema · 2 — Design · ~6 min · MongoDB — Indexing & Performance
What is this?
Wildcard indexes index many arbitrary field paths under a subtree, useful for attribute-heavy or polymorphic documents where you cannot name every field in advance.
Why should you care?
When product attrs or custom tenant fields appear unpredictably, wildcard indexes support queries without constant index redesign — with trade-offs on write cost.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
db.products.insertOne({
name: "Camera",
specs: { megapixels: 24, weatherSealed: true, mount: "E" }
})
db.products.createIndex({ "specs.$**": 1 })
db.products.find({ "specs.megapixels": { $gte: 20 } })
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- specs.$** indexes all subfields under specs.
- The find on megapixels can use the wildcard index.
- You still prefer normal indexes for the hottest known fields.
Practice next
- Insert a product with nested specs.
- Create the wildcard index.
- Query different specs paths.
- Wildcard index customFields.$** on tenants.
- Combine with a regular index on name.
Remember
Wildcard indexes cover unknown subfields. Helpful for flexible specs. Use sparingly — writes get heavier.
Tenant-defined custom fields
Each enterprise customer adds different CRM fields under custom.
Outcome: Support can still filter on those paths without per-tenant indexes.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!