Auto-Delete Certain type of Document in Mongodb in collection

Hello @Sujinthan_Satkunaraj, welcome to the MongoDB Community forum.

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.

This will work using TTL index along with a Partial Index. The documents with FileType = “IDCardCopy” will be deleted after one year, when you specify the Partial Index filter for the “IDCardCopy” FileType. Note the TTL index field must be a BSON Date field.

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?

Yes, the lastModifiedDate must be a BSON Date field.

{ "FileType", typeOfDocument },
{ "UploadTime", DateTime.Now }

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

The above code looks okay to me. The documents of FileType: { $eq: "IDCardCopy" } should be deleted after 900 seconds (15 mins).

Can you verify the field type of UploadTime from the mongo shell using this query?

db.PeopleDocument.aggregate([
  { $project: { UploadTimeType: { $type: "$UploadTime" } }  }
])

The output should be: "UploadTimeType" : "date"



Here is a similar example I had tried from mongo shell and this worked fine. I am using MongoDB v4.2.8.

// Collection 'test' with two documents:
{ _id:1, dt: ISODate("2020-12-07T06:54:12.562Z"), fld: 8 },
{ _id:2, dt: ISODate("2020-12-07T06:54:12.562Z"), fld: 220 }

// Create TTL Index with Partial Index
db.test.createIndex( { dt: 1 }, {
    expireAfterSeconds: 150,    // 2.5 mins
    partialFilterExpression: {
        fld: { $gt: 100 }
    }
});

db.test.getIndexes()
{
	"v" : 2,
	"key" : {
		"dt" : 1
	},
	"name" : "dt_1",
	"ns" : "test.test",
	"expireAfterSeconds" : 150,
	"partialFilterExpression" : {
			"fld" : {
				"$gt" : 100
			}
	}
}

After the index was created, the document with _id: 2 was deleted after the 2.5 mins.