mongoose - change ttl for a single document

依然范特西╮ 提交于 2019-11-30 21:50:49
Luis Sieira

It has been more than a year, but this may be useful for others, so here is my answer:

I was trying accomplish this same thing, in order to allow a grace period after an entry deletion, so the user can cancel the operation afterwards.

As stated by Mike Bennett, you can use a TTL index making documents expire at a specific clock time.

Yo have to create an index, setting the expireAfterSeconds to zero:

db.yourCollection.createIndex({ "expireAt": 1 }, { expireAfterSeconds: 0 });

This will not affect any of the documents in your collection, unless you set expireAfterSeconds on a particular document like so:

db.log_events.insert( {
   "expireAt": new Date('July 22, 2013 14:00:00'),
   "logEvent": 2,
   "logMessage": "Success!"
} )

Example in mongoose

Model

var BeerSchema = new Schema({
  name: {
    type: String,
    unique: true,
    required: true
  },
  description: String,
  alcohol: Number,
  price: Number,
  createdAt: { type: Date, default: Date.now }
  expireAt: { type: Date, default: undefined } // you don't need to set this default, but I like it there for semantic clearness
});

BeerSchema.index({ "expireAt": 1 }, { expireAfterSeconds: 0 });

Deletion with grace period

Uses moment for date manipulation

exports.deleteBeer = function(id) {
  var deferred = q.defer();

  Beer.update(id, { expireAt: moment().add(10, 'seconds') }, function(err, data) {
    if(err) {
      deferred.reject(err);
    } else {
      deferred.resolve(data);
    }
  });
  return deferred.promise;
};

Revert deletion

Uses moment for date manipulation

exports.undeleteBeer = function(id) {
  var deferred = q.defer();
  // Set expireAt to undefined
  Beer.update(id, { $unset: { expireAt: 1 }}, function(err, data) {
    if(err) {
      deferred.reject(err);
    } else {
      deferred.resolve(data);
    }
  });
  return deferred.promise;
};

You could use the expire at clock time feature in mongodb. You will have to update the expire time each time you want to extend the expiration of a document.

http://docs.mongodb.org/manual/tutorial/expire-data/#expire-documents-at-a-certain-clock-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!