This question is mostly popular but with a slight twist; I need to delete few records by when it was created, using its _id
. I do not have any date
, createdAt
fields as I see that mongo uses its _id
for the createdAt timestamp.
How do I delete a recored, say created 30 days ago, using this gist?
const date = new Date();
const daysToDeletion = 30;
const deletionDate = new Date(date.setDate(date.getDate() - daysToDeletion));
const db = <your-database>
db.messages.remove({_id : {$lt : deletionDate}});
The above returns a
CastError
What would work, and as Ive said, I do not have a createdAt field:
db.messages.remove({createdAt : {$lt : deletionDate}});