MongoDB 文件过期太早(猫鼬)
MongoDB documents expiring too soon (mongoose)
我将过期时间设置为 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 指南针中的时间戳时,它与我的时区相匹配。我做错了什么?
我测试了这个:
var TestSchema = new Schema({
name: String,
createdAt: { type: Date, expires: '2m', default: Date.now }
});
文件在第二分钟后被删除,我还确认 TTL 索引已正确创建(默认情况下作为背景索引),TTL 为 120
秒。
试试那个时间格式,看看是否适合你。
Also note that any expected changes to the index via your mongo schema would not be reflected until
you manually remove the previous index and start your app to
auto-create the new one.
MongoDB版本:3.6.5
我将过期时间设置为 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 指南针中的时间戳时,它与我的时区相匹配。我做错了什么?
我测试了这个:
var TestSchema = new Schema({
name: String,
createdAt: { type: Date, expires: '2m', default: Date.now }
});
文件在第二分钟后被删除,我还确认 TTL 索引已正确创建(默认情况下作为背景索引),TTL 为 120
秒。
试试那个时间格式,看看是否适合你。
Also note that any expected changes to the index via your mongo schema would not be reflected until you manually remove the previous index and start your app to auto-create the new one.
MongoDB版本:3.6.5