I'm using mongoose for this.
My Schema is like this:
var UsuarioSchema = new Schema({
email : { type : Email, index : { unique : true}, required : true },
//some other fields, but not required, hopefully, for this sample code
test_expira : { type : Date, default : Date.now, index : { expires : 120 }}
});
When running on the mongodb shell, this is the information that I believe matters:
> db.usuarios.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "dbnamehere.usuarios",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"email" : 1
},
"unique" : true,
"ns" : "dbnamehere.usuarios",
"name" : "email_1",
"background" : true,
"safe" : true
},
{
"v" : 1,
"key" : {
"test_expira" : 1
},
"ns" : "dbnamehere.usuarios",
"name" : "test_expira_1",
"expiresAfterSeconds" : 120,
"background" : true,
"safe" : true
}
]
> new Date();
ISODate("2013-06-12T17:41:43.263Z")
> db.usuarios.findOne({email : 'someemail@gmail.com'});
{
/* some fields go in here! */
"email" : "someemail@gmail.com",
/* some more fields go in here */
"_id" : ObjectId("51b8b087de01a2ec28000002"),
"test_expira" : ISODate("2013-06-12T17:31:51.156Z"),
/* some yet more fields go in here */
"__v" : 0
}
I haven't tested for how long it goes without getting deleted, but even the docs state only a minute overhead should be what one could expect, but not over 15 minutes for a document that should last only 120 seconds.
I'm unsure of how to deal with this. Help will be greatly appreciated :)
EDIT: Version of mongodb being used is v2.4.4 Version of mongoose is 3.0.3
I have tested this feature and it works fine with 2.4.4 MongoDB.
Having taken a closer look at your indexes I realized the problem is a small typo.
My TTL index which works:
{
"v" : 1,
"key" : {
"test_expira" : 1
},
"ns" : "test.usuarios",
"name" : "test_expira_1",
"expireAfterSeconds" : 120,
"background" : true,
"safe" : true
}
Your TTL index which does not work:
{
"v" : 1,
"key" : {
"test_expira" : 1
},
"ns" : "dbnamehere.usuarios",
"name" : "test_expira_1",
"expiresAfterSeconds" : 120,
"background" : true,
"safe" : true
}
Note the correct keyname for TTL index is "expireAfterSeconds" where yours has an extra letter and is "expiresAfterSeconds".