用于文档的Mongoose条件TTL [英] Mongoose conditional TTL for document
本文介绍了用于文档的Mongoose条件TTL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法为某些文档设置 expiresAt
索引,具体取决于当前文档状态?
Is there any way to set expiresAt
index only for some documents, depending on current document state?
推荐答案
是的,有。您需要同时设置 partialFilterExpression 和 expiresAt 索引。仅适用于mongodb 3.2 +
Yes, there is. You need to set both partialFilterExpression and expiresAt indexes. Works only at mongodb 3.2+
以下代码仅在支付
属性等于$ b时才会在24小时后删除文档$ b false
:
Code below will remove document after 24h only if payed
property is equal to
false
:
let billingSchema = new mongoose.Schema({
_id:type:Number,
summ:{
type:Number,
required:true
},
description:String,
payed:{
type:Boolean,
default:false,
index:true
},
ownerId:{
type:mongoose.Schema.Types.ObjectId,
ref:'User',
index:true
}
},{timestamps: true,_id: false});
billingSchema.index({createdAt: 1},{expireAfterSeconds: 24*60*60,partialFilterExpression : {payed: false}});
这篇关于用于文档的Mongoose条件TTL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文