我将到期时间设置为24小时,但文档在大约5-10分钟后到期(我还没有精确地计时)。我做错了什么?我的模式:
const collectionSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
name: {
type: String,
maxLength: 30,
required: true
},
entries: [{ type: mongoose.Schema.Types.ObjectId, ref: "Entry" }],
expireAt: { type: Date, expires: 60 * 60 * 24 }
});在post路由中,我有条件地设置了日期,以便输入的客户端获得数据持久性。
router.post("/", auth, async (req, res) => {
let date = null;
if (!req.user) {
date = new Date();
}
try {
const collection = {
userId: req.body.userId,
name: req.body.name,
expireAt: date
};
const newCollection = await Collection.create(collection);
res.send(newCollection);
} catch (error) {
res.send(error.message);
}
});我以为我有时区问题,但是当我检查MongoDB指南针上的时间戳时,它与我的时区相匹配。我做错了什么?
发布于 2018-09-28 22:04:42
我测试了这个:
var TestSchema = new Schema({
name: String,
createdAt: { type: Date, expires: '2m', default: Date.now }
});Documents在第二分钟后被删除,我还确认TTL索引是正确创建的(默认情况下是背景索引),TTL为120秒。
试试那个时间格式,看看它是否适合你。
还请注意,通过mongo模式对索引的任何预期更改都不会反映出来,直到手动删除前一个索引并启动应用程序来自动创建新索引。
MongoDB版本:3.6.5
https://stackoverflow.com/questions/52558218
复制相似问题