简体   繁体   中英

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

EDIT 1

Actually after further investigation it seems that MogoDB is only holding the most recent 7 records, is this to do with the way i am sending the objects to mongoose, is it overwriting them?

I removed all reference to TTL/Timeout and i only ever see the most recent 7 objects (there are 7 different objects to be stored, but with multiple values over time).

Help greatly appreciated!

Thanks

Gareth

This seems simple enough in the docs, but i can't get it to work.

I have created a template which looks like this:

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'},
});

However upon creating the objects through mongoose, into the Mongolab set the records are only kept for a maximum of 1 minute.

I have tried other variants of the createdAt line also, such as:

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

Now i thought this could be timezone related, but i'm not really sure how to resolve that?

Here is the code i am using to commit the object to 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!')});

Found the solution to this, it seemed that in my testing i had created an index in mono that had a mandatory current timestamp field, this also had a TTL set on it.

So, i had to login to the mongodb and delete this index before it would return to regular behaviour. Reproduced it now every time i have changed the expire time, have to go in, delete all records and delete the index to make things function as needed.

Not sure if this is a bug or not, but it cows me a good few hours of banging my head against a wall there!

Cheers

Gareth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM