有没有办法只为某些文档设置expiresAt
索引,具体取决于当前的文档状态?
答案 0 :(得分:2)
是的,有。您需要设置partialFilterExpression和expiresAt索引。仅适用于mongodb 3.2 +
以下代码仅在payed
属性等于24h后才会删除文档
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}});