Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Creating Indexes  ›  Capstone Project

Expiring (TTL) and Geospatial Indexes

Expiring 15 minA TTL index deletes documents automatically once a date field is older than a set number of seconds, and a 2dsphere index lets you find GeoJSON points near a location, with longitude written before latitude.

Expiring (TTL) and Geospatial Indexes

Two new bookshop features: abandoned shopping carts should clean themselves up, and customers want to see the nearest store. Neither needs application code running on a timer or distance maths in your app. Each needs one special index.

The rule

TTL: a Date plus seconds means delete. 2dsphere: longitude first, then find near.

TTL (time to live) index: a single-field index on a Date field with expireAfterSeconds.

db.carts.createIndex({ updatedAt: 1 }, { expireAfterSeconds: 7200 })   // 2 hours
db.carts.insertOne({ user: "asha", items: [1, 4], updatedAt: new Date() })
  • A background task runs roughly every 60 seconds and deletes documents whose date is older than the limit, so removal is prompt but not exact to the second.
  • The field must hold a BSON Date (or an array of Dates, where the earliest counts). A string or a missing field never expires.
  • For a per-document deadline, index an expireAt field with expireAfterSeconds: 0; each document then expires at its own time.

Geospatial (2dsphere) index: locations stored as GeoJSON points.

db.stores.insertOne({ name: "MG Road", location: { type: "Point", coordinates: [77.6070, 12.9750] } })
db.stores.createIndex({ location: "2dsphere" })

db.stores.find({ location: { $near: {
  $geometry: { type: "Point", coordinates: [77.5946, 12.9716] },
  $maxDistance: 3000                                                 // metres
} } })
  • Coordinates are [longitude, latitude], the reverse of how map apps display them.
  • $near returns results sorted nearest first and requires a geospatial index.
  • $maxDistance is in metres for GeoJSON points.

The shortcut

Before creating either index, check the stored shape: a real Date for TTL, [longitude, latitude] for geo.

Checking the two featuresStep 1 of 4
  1. Cart document

    updatedAt: new Date() is a Date. A TTL index will work.

Drills

Answer fast — trust the rule.

quick drill

A TTL index has expireAfterSeconds: 3600. When is a document removed?

quick drill

A cart has updatedAt: "2025-03-01T10:00:00Z" as a string. What happens with the TTL index?

quick drill

In a GeoJSON point, which value comes first?

quick drill

In what unit is $maxDistance when $near uses a GeoJSON point?

quick drill

How are $near results ordered?

Remember

  • TTL: a Date plus seconds means delete; 2dsphere: longitude first, then find near.
  • Shortcut: check the stored shape, a real Date and [longitude, latitude], before indexing.
  • Trap: string dates never expire, and swapped coordinates fail silently.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainCarts and nearby stores

The bookshop keeps shopping carts that should vanish two hours after they were last touched, and it wants a "stores near me" feature. Explain the two indexes you would create, how each document must look, and one mistake for each feature that would make it silently fail.