Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete MongoDB document at specific time

I am looking into deleting a document at a specific time.

const TestSchema = new Schema({
 expire_at: {
 type: Date,
 },
}, {
 timestamps: true,
});
TestSchema.index({expire_at: 1}, {expireAfterSeconds: 0});

POST

const test = new TestSchema(this.request.body);
  test.expire_at = test.end_time;

  try {
    yield test.save();
  } catch (error) {
    this.status = 409;
    this.response.body = error.errors;
    return;
  }

  this.response.body = test;
  this.status = 201;

It does not seem that the documents delete at the time specified in expire_at.

I am using this Date format: 2016-07-20T05:01:19.567Z

like image 764
safaiyeh Avatar asked Jul 20 '16 04:07

safaiyeh


People also ask

Which MongoDB command is used to remove document from a collection?

MongoDB's remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag. deletion criteria − (Optional) deletion criteria according to documents will be removed.

How do I delete a post in MongoDB?

To delete a record, or document as it is called in MongoDB, we use the deleteOne() method. The first parameter of the deleteOne() method is a query object defining which document to delete.


4 Answers

This will delete the document in two hours:

const TestSchema = new Schema({
    expire_at: {type: Date, default: Date.now, expires: 7200} 
})

//expired in 2 hours
like image 164
emilioriosvz Avatar answered Oct 22 '22 02:10

emilioriosvz


To delete MongoDB document in specific time you can use TTL (time to live). TTL indexes are special single-field indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time.

So you need to create a TTL index like: (mongo shell command)

db.yourCollecName.createIndex({"expire_at": 1 }, { expireAfterSeconds: 5 } );

or you can use mongoose to create this index

TestSchema.createIndex({"expire_at": 1 }, { expireAfterSeconds: 5 } );

then mongodb check after every 60 second and if expire_at date time is less than the current date time then this record will remove after 5 second.

The TTL index does not guarantee that expired data will be deleted immediately upon expiration. There may be a delay between the time a document expires and the time that MongoDB removes the document from the database.

The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.

TTL Indexes

NB: Use createIndex instead of index

like image 25
Shaishab Roy Avatar answered Oct 22 '22 02:10

Shaishab Roy


Deletion process runs after 60 seconds. So document can be there for 59 seconds after the deletion time has been passed.

like image 5
Amit Avatar answered Oct 22 '22 01:10

Amit


As Amit noted, TTL Indexes are not guaranteed to delete data at the time of expiration; the background task to expire data will run every 60 seconds.

The background tasks can also be affected by performance contention and workload, which can cause data to exist far beyond this window:

The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task.

Because the duration of the removal operation depends on the workload of your mongod instance, expired data may exist for some time beyond the 60 second period between runs of the background task.

Source

like image 3
Adam Harrison Avatar answered Oct 22 '22 00:10

Adam Harrison