1. Text index
A "text" index tokenizes the field so $text searches can match words and phrases in body.
db.posts.createIndex({ body: "text" })Typorithm · medium
Step 1 of 8 · intro card
Indexes
Special indexes do more than lookups: { field: "text" } powers search, expireAfterSeconds auto-expires documents (TTL), and partialFilterExpression scopes a unique constraint.
A text index ({ body: "text" }) tokenizes the field so $text queries can search words and phrases.
A TTL index adds expireAfterSeconds to a date field, and MongoDB deletes each document that many seconds after its timestamp — ideal for sessions or logs.
A partial index with partialFilterExpression only indexes matching documents, which lets a unique constraint apply to just the rows you care about.
{ field: "text" } enables $text search.
expireAfterSeconds makes a TTL index that auto-deletes.
partialFilterExpression scopes which docs are indexed.
db.posts.createIndex({ body: "text" })
db.sessions.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })
db.users.createIndex({ email: 1 }, { unique: true, partialFilterExpression: { email: { $exists: true } } })Connect each part of the code to the concept before you type.
A "text" index tokenizes the field so $text searches can match words and phrases in body.
db.posts.createIndex({ body: "text" })expireAfterSeconds makes a TTL index that deletes documents 3600s after their createdAt date.
{ expireAfterSeconds: 3600 }partialFilterExpression indexes only matching documents, so the unique rule applies only where email exists.
partialFilterExpression: { email: { $exists: true } }unique still enforces no duplicate emails, but here only across the partially indexed documents.
unique: trueRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.