当我请求将新对象保存到我的mongodb时,它被保存,并且几秒钟后该架构中的所有内容都消失了。
在下面的屏幕截图中,您可以看到这种情况,在第一个命令中,我检查架构是否为空,然后我请求保存一个成功完成的新对象,几秒钟后,您可以看到该对象具有不见了
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 docs TTL index