猫鼬 - 更改单个文档的 ttl [英] mongoose - change ttl for a single document
问题描述
我有一件非常确定的事情要完成,并且在我自己编写整个代码之前,我想确保它在 mongoose/mongoDB 中是不可能的.我检查了 mongoose-ttl 的 nodejs 和几个论坛,但没有找到我需要的东西.这是:
我有一个带有日期字段 createDate 的架构.现在我希望在该字段上放置一个 TTL,到目前为止还不错,我可以这样做(5000 秒后到期):createDate: {type: Date, default: Date.now, expires: 5000}
但我希望我的用户能够对他们喜欢的文档进行投票",这样这些文档将有更长的生命周期,而无需更改我收藏中的其他文档.那么,一旦用户告诉我他喜欢使用 mongoose 或其他现有 npm 相关模块的文档,我可以以某种方式更改单个文档的 TTL 吗?
谢谢
已经一年多了,但是这可能对其他人有用,所以这里是我的答案:
我试图完成同样的事情,以便在条目删除后允许一个宽限期,以便用户可以在之后取消操作.
正如 Mike Bennett 所述,您可以使用 TTL 索引 使文档在特定时钟时间过期.>
你必须创建一个索引,将 expireAfterSeconds
设置为零:
db.yourCollection.createIndex({ "expireAt": 1 }, { expireAfterSeconds: 0 });
这不会影响您集合中的任何文档,除非您像这样在特定文档上设置 expireAfterSeconds:
db.log_events.insert( {"expireAt": new Date('July 22, 2013 14:00:00'),日志事件":2,"logMessage": "成功!"})
猫鼬中的例子
型号
var BeerSchema = new Schema({名称: {类型:字符串,独特:真实,要求:真实},描述:字符串,酒精:数字,价格:数量,createdAt: { 类型:日期,默认值:Date.now }expireAt: { type: Date, default: undefined }//你不需要设置这个默认值,但我喜欢它的语义清晰});BeerSchema.index({ "expireAt": 1 }, { expireAfterSeconds: 0 });
宽限期删除
使用 moment 进行日期操作
exports.deleteBeer = function(id) {var deferred = q.defer();Beer.update(id, { expireAt: moment().add(10, 'seconds') }, function(err, data) {如果(错误){deferred.reject(err);} 别的 {deferred.resolve(data);}});返回 deferred.promise;};
恢复删除
使用 moment 进行日期操作
exports.undeleteBeer = function(id) {var deferred = q.defer();//设置 expireAt 为 undefinedBeer.update(id, { $unset: { expireAt: 1 }}, function(err, data) {如果(错误){deferred.reject(err);} 别的 {deferred.resolve(data);}});返回 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屋!