我使用貓鼬這是我的架構:
const ShortUrlModel = mongoose.model(
'ShortUrl',
new mongoose.Schema({
original_url: String,
hash: String,
expires_at: { type: Date, expires: 5000 },
}),
);
我在 5 秒后過期進行測驗,但它不起作用
我想在 10 天內到期。
發布到我的服務器以添加 url 時
fastify.post('/new', async (request, reply) => {
let original_url = request.body.url;
let hash = request.body.hash;
const today = new Date();
const expires_at = new Date();
expires_at.setDate(today.getDate() 10);
let short = await ShortUrlModel.findOne({ hash });
if (short) {
...
} else {
const newShortener = new ShortUrlModel({
original_url,
hash,
expires_at,
});
let saveUrl = await newShortener.save();
console.log(saveUrl);
reply.send({
...
});
}
});
uj5u.com熱心網友回復:
該領域是expireAfterSeconds
const ShortUrlModel = mongoose.model(
'ShortUrl',
new mongoose.Schema({
original_url: String,
hash: String,
}, {
expireAfterSeconds: 5000
}),
);
另請注意,如果更改秒數,則需要洗掉索引并重新創建索引。更改不會僅僅因為您更改了架構宣告中的秒數而發生。
更多關于更改過期在這里
注意 2:Mongodb 中的檔案過期不會立即發生(例如正好 5 秒)。這在 mongodb檔案中有說明。
TTL 索引不保證過期資料會在過期后立即被洗掉。檔案過期與 MongoDB 從資料庫中洗掉檔案的時間之間可能存在延遲。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/512267.html
