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?