Auto-Delete Certain type of Document in Mongodb in collection

Viewed 197

I have a collection called PeopleDocument. This collection contains three different types of files: IDCardCopy, taxCopy, PermitCopy. Users can upload any of these files. I want to autodelete IDCardCopy one year after it was uploaded. I am looking at MongoDB TTL, however I have some questions:

db.PeopleDocument.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 31622400} ) 

If I create an index like the one above, I think it will delete all files in PeopleDocument after 1 year, is it possible to only delete IDCardCopy?

More detail:

This is a C# code I use to insert Document:

var collInternDocuments = _database.GetCollection<BsonDocument>("PPLDocuments");



var doc = new BsonDocument{
                             {"Documentid", strDocumentID},
                             { "FileType", typeOfDocument},                                        
                             { "FileContent", file.ContentType},
                             { "FileName", file.FileName},
                             { "UploadTime", DateTime.Now},
                             { "UploadedBy", uploadedBy},
                             { "GridFSFileID", strGridFSFileID}
                           };
    

collInternDocuments.UpdateOne( Builders<BsonDocument>.Filter.Eq("_id", internUserID), Builders<BsonDocument>.Update.AddToSet("Documents", doc));
           

This is how I created Index:

db.PeopleDocument.createIndex( {UploadTime:1}, 
                               {
                                  expireAfterSeconds:900,
                                  partialFilterExpression:{
                                                            FileType:{$eq:"IDCardCopy"}
                                                            }
                                })

This is the results for db.PeopleDocument.getIndexes():

 {
    "v":2,
    "key":{
           "_id":1
           },
     "name" :"_id_",
      "ns" : "people.PeopleDocument"
},
{ 

 "v":2,
    "key":{
           "UploadTime":1
           },
     "name" :"UploadTime_1",
      "ns" : "people.PeopleDocument",
      "expireAfterSeconds":900,
      "partialFilterExpression":{
               "FileType": { "$eq":"IDCardCopy"
                 }
           }
 }
          

This didn't delete the file after 900 sec, could this be a date issue?

3 Answers

this works for me. after 10secods (not exactly due to the deletion thread running every 60seconds), the IDCardCopy document is gone.

db.PeopleDocument.createIndex(
    { "uploadTime": 1 },
    {
        expireAfterSeconds: 10,
        partialFilterExpression: { "FileType": { $eq: "IDCardCopy" } }
    }
)
db.PeopleDocument.insertMany(
    [
        {
            uploadTime: new ISODate(),
            FileType: "IDCardCopy"
        },
        {
            uploadTime: new ISODate(),
            FileType: "taxCopy"
        },
    ]
)

make sure you're setting the uploadTime field to the correct UTC now time in your application.

I think you should add in PeopleDocument the column expiresAt and fill this field on creating concrete record depended on your file type. And then simply create index like that:

db.PeopleDocument.createIndex( { "expiresAt": 1 }, { expireAfterSeconds: 0} ) 

I solved this by creating a C# console app that will delete documents created one year ago, I created a Task Scheduler that will run everyday to run this app.

Related