MongoDB TTL 인덱스 (자동 문서 삭제)1
// 데이터 삽입
db.collection.insertOne({
name: 'Temporary',
expireAt: new Date('2024-11-30T00:00:00Z'),
})
// TTL 인덱스 생성 (expireAt 시간 기준 삭제)
db.collection.createIndex({ expireAt: 1 }, { expireAfterSeconds: 0 })
SQLite는 TTL 미지원 → 트리거 또는 외부 스케줄러로 구현
Footnotes
-
Expire Data from Collections by Setting TTL | MongoDB: “To expire documents at a specific clock time, create a TTL index on a field that holds values of BSON date type or an array of BSON date-typed objects, and specify an
expireAfterSecondsvalue of0.” ↩ -
TTL Indexes | MongoDB: “The background task that removes expired documents runs every 60 seconds.” ↩
-
TTL Indexes | MongoDB: “If the indexed field in a document doesn’t contain one or more date values, the document will not expire.” ↩