I'm trying to archive data which is older than 4 months in a mongodb collection. There are existing documents in the collection.
As per mongodb, for standard collections we can either use date fields or custom queries. Since _id
is by default indexed and contains ObjectId(includes timestamp value), I'm thinking of creating custom query which does $lt
operation on the 4 months older ObjectId value:
{"_id": {"$lt": ObjectId(Math.floor(new Date().setMonth(new Date().getMonth() - 4) / 1000).toString(16) +"0000000000000000") }}
But, is this the efficient way? Is it better to add new date field in the existing collection documents and then do archival based on it or use _id
anyways as this is already indexed to archive the documents?