mongodb+node数据过期时间

记录一下学习过程中遇到的问题,百度到很多文章发现都没有node的或者是有各种问题例如数据在1分钟内就删除了

解决:

// tokendb.js

const mongoose = require("mongoose");

//net start mongodb

var moment = require('moment');

mongoose.connect("mongodb://localhost/picsdb", { useNewUrlParser: true, useUnifiedTopology: true })

    .catch((error) => console.log(error));

const tokendbSchema = new mongoose.Schema({

    tokenid: {

        type: String,

    },

    createAt: {

        type: Date,

    }

})

const tokendb = mongoose.model('tokendb', tokendbSchema);

console.log(tokendbSchema.index)

//下面的代码是设置过期时间的关键代码,上面的createAt必须是Date类型

tokendb.createIndexes(tokendbSchema.index({ createAt:new moment().valueOf()}, { expireAfterSeconds: 0}), function (err, info) {})

module.exports.tokendb = tokendb

//路由 index.js

         tokendb.create({

                        tokenid: token,

                        createAt:moment().add(120, 'minutes').toDate(),//120分钟后过期,实际上可能有十几秒的延迟

                    })

转自MongoDB TTL到期在NodeJS上无法正常工作-Java 学习之路我使用MongoDB(v3.4)作为缓存并使用TTL索引使记录失效 . 但是,TTL设置似乎无法正常工作 . 具体来说,我使用 endpoints 测试插入数据(如下所示) . endpoints mongoInsert应该在5分钟后...https://www.javaroad.cn/questions/146078

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值