Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
959 views
in Technique[技术] by (71.8m points)

mongoose - change ttl for a single document

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...