mongoose - change ttl for a single document

强颜欢笑 提交于 2019-11-30 17:59:34

问题


I have a very certain thing i want to accomplish, and I wanted to make sure it is not possible in mongoose/mongoDB before I go and code the whole thing myself. I checked mongoose-ttl for nodejs and several forums and didn't find quite what I need. here it is:

I have a schema with a date field createDate. Now i wish to place a TTL on that field, so far so good, i can do it like so (expiration in 5000 seconds): createDate: {type: Date, default: Date.now, expires: 5000}

but I would like my users to be able to "up vote" documents they like so those documents will get a longer period of time to live, without changing the other documents in my collection. So, Can i change a TTL of a SINGLE document somehow once a user tells me he likes that document using mongoose or other existing npm related modules?

thank you


回答1:


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;
};



回答2:


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



来源:https://stackoverflow.com/questions/22106271/mongoose-change-ttl-for-a-single-document

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