【发布时间】: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