Skip to content
Provides time-to-live support for Mongoose
Find file
Latest commit de2cfae May 12, 2014 Aaron Heckmann hey travis!
Failed to load latest commit information.
lib compat with node 0.8 and mongoose 3 Jun 25, 2012
test get rid of should.js Jun 25, 2012
.gitignore initial commit Jan 19, 2012
.travis.yml hey travis! May 12, 2014 release 0.0.3 Jun 25, 2012
LICENSE release 0.0.1 Jan 24, 2012
Makefile remove another should ref Jun 25, 2012 Added magic ttl variable May 1, 2014
index.js initial commit Jan 19, 2012
package.json release 0.0.3 Jun 25, 2012

Mongoose-TTL Plugin

Provides time-to-live support for Mongoose.

Build Status


  • ttl: the time each doc should live in the db (default 60 seconds)
  • interval: how often the expired doc reaper runs (default 5 mins)
  • reap: enable the expired doc reaper (default true)
  • onReap: callback passed to reaper execution


var ttl = require('mongoose-ttl');
var schema = new Schema({..});
schema.plugin(ttl, { ttl: 5000 });

The ttl option supports the ms module by guille meaning we can specify ttls with friendlier syntax. Example:

 value     milliseconds
 '2d'      172800000
 '1.5h'    5400000
 '1h'      3600000
 '1m'      60000
 '5s'      5000
 '500ms'   500
 100       100

The expired document reaper can be disabled by passing reap: false. Useful when working in multi-core environments when we only want one process executing it.

var ttl = require('mongoose-ttl');
var schema = new Schema({..});
schema.plugin(ttl, { ttl: 5000, reap: false });
var Cache = db.model('Cache', schema);
if (isMyWorker) Cache.startTTLReaper();

The reaper can also be stopped.


Time-to-live is specified at the collection level, however it can also be overridden for a given document.

var cache = new Cache;
cache.ttl = '2m' // lives for two minutes;

We can also reset the ttl for a given document to its default plugin state.


Mongoose Version

= 2.5.2

MongoDB TTL collections

MongoDB >= 2.2 has official support for TTL collections. The official feature is not quite as flexible as this plugin but offers alternative benefits a plugin cannot. Consider reading the docs and making an informed choice.


Something went wrong with that request. Please try again.