猫鼬-更改单个文档的ttl [英] mongoose - change ttl for a single document

查看:90
本文介绍了猫鼬-更改单个文档的ttl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很确定的事情要完成,我想确保在我自己编写整个代码之前,在mongoose/mongoDB中是不可能的. 我在mongoose-ttl上检查了nodejs和几个论坛,但没有找到我需要的东西. 在这里:

我有一个带有日期字段createDate的架构.现在,我希望将TTL放置在该字段上,到目前为止,我可以这样做(5000秒后过期): createDate:{类型:日期,默认值:Date.now,到期日:5000}

但是我希望我的用户能够对其喜欢的文件进行投票",这样这些文件才能有更长的生存时间,而无需更改集合中的其他文件. 因此,一旦用户告诉我他喜欢使用猫鼬或其他现有的与npm相关的模块,就可以以某种方式更改SINGLE文档的TTL吗?

谢谢

解决方案

已经一年多了,但这可能对其他人有用,所以这是我的答案:

我试图完成同样的事情,以便在条目删除后留出宽限期,以便用户随后可以取消操作.

Mike Bennett 所述,您可以使用TTL索引 db.yourCollection.createIndex({ "expireAt": 1 }, { expireAfterSeconds: 0 });

这不会影响您馆藏中的任何文档,除非您像这样对特定文档设置expireAfterSeconds:

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

猫鼬示例

型号

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

具有宽限期的删除

使用时刻进行日期操作

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

还原删除

使用时刻进行日期操作

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

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

解决方案

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

这篇关于猫鼬-更改单个文档的ttl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆