gpt4 book ai didi

node.js - Mongodb 模式在几秒后清空

转载 作者:可可西里 更新时间:2023-11-01 09:38:55 25 4
gpt4 key购买 nike

当我请求将新对象保存到我的 mongodb 时,它被保存了,几秒钟后该模式中的所有内容都消失了。
在下面的屏幕截图中,您可以看到这种情况,我使用第一个命令检查模式是否为空,然后我请求保存一个成功完成的新对象,几秒钟后您可以看到该对象有消失了。 See screenshot here
express 端点如下所示:

router.post('/bookdate',passport.authenticate('jwt', {session:false}), (req, res) => {
const userId = req.user._id
const appartmentNumber = req.user.apartmentNumber;
const requestedDate = req.body.requestedDate;
const bookingZone = req.body.bookingZone;

const newBooking = new Booking({
'apartmentNumber': appartmentNumber,
'dateOfBooking': requestedDate,
'bookingZone': bookingZone
});

if (req.user.hasTimeBooked) {
res.json({booked: false, msg: 'There is already a booking for this user.'})
} else {
if (typeof newBooking.requestedDate !== undefined && typeof newBooking.bookingZone !== undefined) {
Booking.addBooking(newBooking, (err, result)=>{
if(err){
res.json({booked: false, msg: err})
} else {
res.json({booked: true, msg: result})
}
})
} else {
res.json({booked: false, msg: 'Undefined parameters Date or Zone'})
}
}
});

Mongoose 模式看起来像这样

const mongoose = require('mongoose');
const config = require('../config/database');
const BookingSchema = mongoose.Schema({
apartmentNumber:{
type: Number,
unique: true
},
dateOfBooking:{
type: Date
},
bookingZone:{
type: String
}
});
const Booking = module.exports = mongoose.model('bookings',BookingSchema, 'bookings');

module.exports.addBooking = function(bookingObj, cb){
var newBooking = new Booking(bookingObj);
newBooking.save(cb);
}

控制台中没有出现任何错误,我不太确定从哪里开始查找。提前致谢!

编辑db.bookings.getIndices() 的结果显示在屏幕截图中 here

最佳答案

getIndices 输出我可以看到你已经在 dateOfBooking 上创建了一个 TTL 索引,因此它在后端 60 秒后被删除

来自 mongo 文档 TTL index

TTL indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time or at a specific clock time

关于node.js - Mongodb 模式在几秒后清空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48485865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com