I tried this in my end and it works. How to you create new ObjectId
? Can you try it with Mongoose?
const mongoose = require('mongoose');
let remove = await mongoose.connection.db.collection("mycollection").deleteMany({
_id: {
$lt: mongoose.Types.ObjectId(
Math.floor(new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) / 1000).toString(16) + '0000000000000000'
),
}
});