【问题标题】:why is mongoose TTL deleting document, even when partial filter does not match the option为什么猫鼬TTL删除文档,即使部分过滤器与选项不匹配
【发布时间】:2020-06-09 20:01:26
【问题描述】:

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

谢谢


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

partial filer不应该让为true的authorPremium被删除,只有delete的是authorPremium为false...请指教。

从 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 设置并确保只有我想要的那些?

【问题讨论】:

  • 您能否检查一下您的收藏是否已成功创建索引?
  • @mickl ,我不知道该怎么做,我放弃了,只是尝试添加一个 ttl 选项以在 14 天后删除帖子,但几分钟后将其删除!我搜索了文档和所有论坛,在这里真的迷失了。这就是我现在正在尝试的postSchema.index({createdAt: 1},{expireAfterSeconds: 60*60*24*14});
  • 尝试打开 mongo shell 并运行db.posts.getIndexes()(假设您的集合名称是posts)查看是否存在partialExpression - 您的代码看起来有效
  • 我将使用返回更新帖子,但它似乎保留了我的旧架构索引配置,即使我将它设置为一些新的配置,就像你刚才看到的那样

标签: javascript mongodb mongoose mongodb-query mongoose-schema


【解决方案1】:

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

有关.index() 的工作原理以及为什么引入.syncIndexes() 的更多信息,请访问here

【讨论】:

  • 我尝试使用它,但我没有创建模型,我创建了一个模式,你可以看到我的设计,我尝试使用 cmd 与模式对话,但我得到了错误.. .
  • 我想我会删除 ttl 功能,对我来说没有意义,找不到解释,看起来太有问题而且不起作用,如果时间戳早于 x,我可以做一个简单的查询来删除帖子用户 x 和用户 y 的天数,这是一个很好的概念 ttl,但在我看来它太不可靠和不稳定。
  • 您不能从 cmd 与架构对话,因为架构是特定于猫鼬的。 module.exports = mongoose.model('Post', postSchema); 是您创建模型的时刻,您也可以从 mongo shell 手动删除旧索引以创建新索引,但我建议使用 syncIndexes
  • 我会在哪里使用同步索引,在某个地方我已经导入了模型?
  • 是的,您可以在导入模型并启动应用程序时运行一次
猜你喜欢
  • 2021-01-21
  • 2021-05-16
  • 2023-04-02
  • 1970-01-01
  • 2016-03-22
  • 2019-06-06
  • 2013-09-14
  • 2016-04-20
相关资源
最近更新 更多