I am new to Node js and MongoDB (coming from an SQL background). I am trying to implement a function that removes all the records in a collection 'journeys' where the timestamp is older than 10 mins. This is what I have done so far:
connection.open(function(err, connection) {
var database = connection.db(DATABASE);
database.collection(JOURNEY_COLLECTION, {}, function(err, journeys) {
var now = new Date().getTime() / 1000 - 10 * 60;
journeys.remove({timestamp : ???}, function(err, result) {
if(err != null)
console.log('A refresh error occurred');
connection.close();
});
});
});
Am I on the right track? What should I be putting in place of the "???" ?
I'm sure someone'll post an answer that uses the example code, but I'm going to show you how to do it on a database level.
First, define a field that'll contain a Date. This can be either the creation time (method A) or when you want it removed (method B). I'll explain both, but method B is my favourite. It allows you to more easily modify the time to live. A field with the date set to null
will not be removed.
Method A:
var someDocument = { "created": new Date() };
Method B:
var someDocument = { "expire": new Date(Date.now() + 10 * 60 * 1000) };
Add a TTL index on this field, like so:
Method A:
db.myCollection.ensureIndex( { "created": 1 }, { expireAfterSeconds: 600 } )
Method B:
db.myCollection.ensureIndex( { "expire": 1 }, { expireAfterSeconds: 0 } )
MongoDB runs a job every 60 seconds that looks for documents that should be removed. Due to this reason, you might get documents that are slightly stale.
(Note that I've named the field 'created' (method A) / 'expire' (method B), you can name them anything but I think this is more descriptive than just 'timestamp' for these examples )
You're on the right track.
Your "???" should be in the form {$lt:varName}
var tenMinutesOld = new Date()
tenMinutesOld.setMinutes(tenMinutesOld.getMinutes()-10)
journeys.remove({timestamp: {$lt:tenMinutesOld}}, function(err, result) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With