为什么猫鼬 TTL 删除文档,即使部分过滤器与选项不匹配

why is mongoose TTL deleting document, even when partial filter does not match the option

我的部分过滤器正在删除文档,但用户不符合该要求,我是否错误地使用了部分过滤器?

谢谢


const postSchema = new mongoose.Schema(
    {
        title: { type: String },
        description: { type: String },
        image: { type: String },
        price: { type: String },
        location: { type: String },
        image: { type: Array },
        author: {
            type: String,
            ref: 'User'
        },
        authorPremium: {
            type: Boolean,
            default: true,
            index:true
        },
        reported: {
            type: Boolean,
            default: false
        },
        reportClear: {
            type: Boolean,
            default: false
        }
    },
    {
        timestamps: true
    }
);

// users who are not premium will have posts deleted after 20 seconds
postSchema.index({ createdAt: 1 }, { expireAfterSeconds: 20, partialFilterExpression: { authorPremium: false } });

module.exports = mongoose.model('Post', postSchema);

部分文件管理器应该不允许删除为true的authorPremium,只删除为false的authorPremium...请指教。

return 来自 mongo 索引

[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.posts"
        },
        {
                "v" : 2,
                "key" : {
                        "createdAt" : 1
                },
                "name" : "createdAt_1",
                "ns" : "test.posts",
                "expireAfterSeconds" : 120,
                "background" : true
        },
        {
                "v" : 2,
                "key" : {
                        "authorPremium" : 1
                },
                "name" : "authorPremium_1",
                "ns" : "test.posts",
                "background" : true
        },
        {
                "v" : 2,
                "key" : {
                        "timestamps" : 1
                },
                "name" : "timestamps_1",
                "ns" : "test.posts",
                "expireAfterSeconds" : 20,
                "background" : true
        }
]

当我使用 mongo cmd 时,我的一些旧设置似乎保留了下来……还有一些新设置?那么我如何在测试时完全清除这些旧的 ttl 设置并确保只有我想要的设置在那里?

您的 .index(...) 似乎不起作用,因为您在 createdAt 字段上已有旧索引,而 mongoose 不会删除旧索引。要将索引与您的架构同步,您可以使用 Model.syncIndexes

有关 .index() 的工作原理以及引入 .syncIndexes() 的原因的更多信息,请参见 here