50

Below is the command that can be used via the mongo terminal to set an expiry time for collections (a TTL):

db.log.events.ensureIndex( { "status": 1 }, { expireAfterSeconds: 3600 } )

How do I do this from my code in Node.js using mongoose?

ZachB
  • 10,966
  • 2
  • 53
  • 85
Amanda G
  • 1,833
  • 9
  • 32
  • 43

5 Answers5

120

In Mongoose, you create a TTL index on a Date field via the expires property in the schema definition of that field:

// expire docs 3600 seconds after createdAt
new Schema({ createdAt: { type: Date, expires: 3600 }});

Note that:

  • MongoDB's data expiration task runs once a minute, so an expired doc might persist up to a minute past its expiration.
  • This feature requires MongoDB 2.2 or later.
  • It's up to you to set createdAt to the current time when creating docs, or add a default to do it for you as suggested here.
    • { createdAt: { type: Date, expires: 3600, default: Date.now }}
JohnnyHK
  • 290,447
  • 61
  • 595
  • 453
  • ```updateLogSchema = mongoose.Schema({ success: { type: Boolean, required: true }, saveDate: { type: Date, required: true, default: Date.now, expires: 60*60*24*7 #Every 7 days } })``` So I have to do this on one property which is a date? What happens if I update my doc & my saveDate, does it live 7 days again? – Andi Giga Nov 26 '15 at 16:28
  • 1
    @AndiGiga you probably want to review the mongo docs: https://docs.mongodb.org/manual/tutorial/expire-data/ – mooreds Jan 27 '16 at 00:04
  • 1
    How can I apply ttl in specific elements of array. I am writing node API. I am persisting token in string array I want to remove those after a specified time interval ?? – Sunil Sharma Feb 22 '16 at 07:42
  • 2
    So if I set the expires to `updatedAt`, then it will refresh the count every time after the value updated? – Haven Jul 04 '16 at 21:14
  • but then how do i know when it expired? where is it written in the db? – dang Nov 16 '16 at 19:14
  • 2
    I'm following this approach, however no matter the amount of seconds I indicate, it deletes the document after one minute. `cartSchema.index({createdAt: 1},{expireAfterSeconds: 14400})` – George Cscnt Jun 28 '18 at 21:00
  • this solution does not work and there's no mention of this approach in the Mongoose docs. Use the approach outlined here: https://docs.mongodb.com/manual/tutorial/expire-data/ – Nick Feb 24 '19 at 07:54
  • @Nick Documentation for this feature in Mongoose is [here](https://mongoosejs.com/docs/api.html#schema_Schema-index). It does work, but you need drop any existing index on the field so that Mongoose can create it. – JohnnyHK Feb 24 '19 at 14:58
  • @JohnnyHK in your OP you use this line: `new Schema({ createdAt: { type: Date, expires: 3600 }});` which as far as I have tested does not work and is not mentioned in the docs. The structure mentioned in the docs - `new Schema({ date: { type: Date, index: { unique: true, expires: '1d' }})` - may indeed work. – Nick Feb 25 '19 at 10:57
  • @Nick I just tried your `createdAt` schema with Mongoose 5.1.4 and the expiration worked correctly. Can you post a new question if you're still having problems with it? – JohnnyHK Feb 25 '19 at 14:56
35

this code is working for me.

may it help

let currentSchema = mongoose.Schema({
    id: String,
    name: String,
    packageId: Number,
    age: Number
}, {timestamps: true});

currentSchema.index({createdAt: 1},{expireAfterSeconds: 3600});
Sadegh Teimori
  • 1,190
  • 10
  • 12
19

Providing a string to expires also works nicely with Mongoose if you do not want to deal with the expire time calculation and improve the overall readability of the schema.

For example here we are setting the expires to 2m (2 minutes) and mongoose would convert to 120 seconds for us:

var TestSchema = new mongoose.Schema({
  name: String,
  createdAt: { type: Date, expires: '2m', default: Date.now }
});

Mongoose would create an index in the background and auto set the expireAfterSeconds to in this case 120 seconds (specified by the 2m).

It is important to note that the TTL process runs once every 60 seconds so it is not perfectly on time always.

Akrion
  • 17,012
  • 1
  • 30
  • 48
  • Thank you for this answer, but can you tell me if I can use `expires: '2h'` for hours or `'2d'` for days – Naveen Kumar May 20 '22 at 17:36
  • it works but I don't know why it deletes at later time, even when I set `expires` to `1 (1 second)`, it takes `30seconds` to delete the document, and I saw in atlas, it showed me ttl time 1second – Naveen Kumar May 20 '22 at 18:46
3

If you are working with Mongodb Atlas Replica Sets - try:

import * as mongoose from 'mongoose'; 

let currentSchema = new mongoose.Schema({
        createdAt: { type: Date, expires: 10000, default: Date.now },
        id: String,
        name: String,
        packageId: Number,
        age: Number
        });

currentSchema.index({"lastModifiedDate": 1 },{ expireAfterSeconds: 10000 });
Jason Mullings
  • 744
  • 11
  • 7
0

There is a npm library - 'mongoose-ttl'.:

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

you can see all the options of this library: https://www.npmjs.com/package/mongoose-ttl

arr
  • 9
  • 1