使用Mongoose/Node.js在对象上设置TTL/Expires

Setting TTL/Expires on object using Mongoose/Node.js

本文关键字:设置 TTL 对象 Expires Mongoose Node js 使用      更新时间:2024-05-25

编辑1

事实上,经过进一步调查,MogoDB似乎只保存了最近的7条记录,这与我将对象发送给猫鼬的方式有关吗?是不是覆盖了它们?

我删除了所有对TTL/Timeout的引用,只看到了最近的7个对象(有7个不同的对象要存储,但随着时间的推移有多个值)。

非常感谢您的帮助!

感谢

Gareth

这在文档中看起来很简单,但我无法让它发挥作用。

我创建了一个模板,看起来像这样:

var trackingSchema = new mongoose.Schema({
VehicleID: {type: String, min: 0}, 
Label: {type: String, min: 0},
RegNumber: {type: String, min: 0},
VehPosDateTimeUTC: {type: String, min: 0},
Latitude: {type: String, min: 0},
Longitude: {type: String, min: 0},
Heading: {type: String, min: 0},
SpeedKPH: {type: String, min: 0},
OdometerKM: {type: String, min: 0},
SpeedMPH: {type: String, min: 0},
OdometerMiles: {type: String, min: 0},
EventCode: {type: String, min: 0},
GpsStrength: {type: String, min: 0},
Owner: {type: String, min: 0},
DriverName: {type: String, min: 0},
DriverID: {type: String, min: 0},
EventCodeId: {type: String, min: 0},
EventType: {type: String, min: 0},
createdAt: { type: Date, default : Date.now, expires: '1d'},
});

然而,在通过猫鼬创造这些物体后,进入蒙古人的记录最多只保存了1分钟。

我也尝试过createdAt行的其他变体,例如:

createdAt: { type: Date, default : Date.now, expires: 6000 }

现在我认为这可能与时区有关,但我真的不确定如何解决这个问题?

以下是我用来将对象提交给mongoose的代码:

// Creating one vehicle record.
var vehicleData = new   trackingRecord ({
VehicleID:   responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].VehicleID[0], 
Label: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].Label[0],
RegNumber: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].RegNumber[0],
VehPosDateTimeUTC:responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].VehPosDateTimeUTC[0],
Latitude: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].Latitude[0],
Longitude: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].Longitude[0],
Heading: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].Heading[0],
SpeedKPH: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].SpeedKPH[0],
OdometerKM: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].OdometerKM[0],
SpeedMPH: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].SpeedMPH[0],
OdometerMiles: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].OdometerMiles[0],
EventCode: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].EventCode[0],
GpsStrength: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].GpsStrength[0],
Owner: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].Owner[0],
DriverName: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].DriverName[0],
DriverID: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].DriverID[0],
EventCodeId: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].EventCodeId[0],
EventType: responseJSON.VehPosResponse.position_list[0].CVehiclePos[i].EventType[0]
                                });
// Saving it to the database.
                                vehicleData.save(function (err) {if (err) console.log ('Error on save!')});

找到了解决方案,在我的测试中,我似乎在mono中创建了一个索引,该索引有一个强制性的当前时间戳字段,上面还设置了TTL。

因此,我必须登录mongodb并删除该索引,然后它才能恢复正常行为。现在,每次我更改过期时间,都要重新复制它,必须进去,删除所有记录并删除索引,以使事情按需运行。

不确定这是不是一只虫子,但它让我在那里用头撞墙好几个小时!

干杯

Gareth