0

Hi i came across one concept called as ttl in mongodb and tried the same in a insert operation using mongoose.Below is the code

mongoose = require('mongoose');
var Schema = mongoose.Schema;
var META_CTS_DB = mongoose.createConnection('mongodb://localhost/META');
var ttl= require('mongoose-ttl');
var date1=new Date();
var ServiceSchema = new Schema({
_id:String, 
CURDATE:{type:Date,index:true},
SERVICE_PROTOCOL:String, 
});
ServiceSchema.plugin(ttl, { ttl:5000 });
var MasterDBdata = META.model('ServiceSchema',ServiceSchema,'API_KEY_91994094177912_SERVICE_ID_0008_SERVICE_CONFIG');
var item = new MasterDBdata({'_id':'SERVICE_ID_0006','CURDATE':date1,'SERVICE_PROTOCOL':'Nil'});
item.save();

The ttl worked fine for this and the collection did get deleted.But when i performed the same for a update function as below

MasterDBdata.update({'_id':'SERVICE_ID_0006'},{'CURDATE':date1,'SERVICE_PROTOCOL':'hhl'},{upsert:true},function (err){

                                                        if (err) return handleError(err);
                                                        console.log('\n data stored in Database...')});

The ttl function didnt seem to work properly.Any idea regarding the behaviour of ttl and its performance on these functions will be really helpful.Thanks in advance

Amanda G
  • 1,931
  • 10
  • 33
  • 43

1 Answers1

1

The mongoose-ttl plug-in uses its own hidden __ttl field to track a document's expiration time, so updating your own CURDATE field isn't going to have any effect.

If you're on MongoDB 2.2 you should be using the native support for TTL collections instead of the plug-in, defining CURDATE as the expires key field as discussed in your previous question here.

Community
  • 1
  • 1
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • I tried the following example var schema = new Schema({ createdAt: Date }); schema.path('createdAt').expires('5s'); Bu I got error as follows schema.path('createdAt').expires('5s'); ^ TypeError: Object # has no method 'expires'.Any idea regarding this will be helpful – Amanda G Feb 04 '13 at 11:07
  • @AmandaG What version of Mongoose are you using? Run `npm update mongoose` to update to the latest as this is a fairly recent addition. – JohnnyHK Feb 04 '13 at 13:53