Home > Blockchain >  is there any way to delete old documents in mongoDB using mongoose
is there any way to delete old documents in mongoDB using mongoose

Time:08-16

hey i need to delete old documents in a collection in mongoDB using mongoose package I'm currently using mongoose version 6.4.0 and mongoDB version v5.0.5-rc0 and i have tried all answers given bellow in this question

i need to know if there any way to full fill my requirement using mongoose

CodePudding user response:

It depends on your schema of course, but a general structure of an invoked mongoose is:

const doDelete = async () => {
  var response = await Model.deleteMany({some_parameter:"some_value"});
}

doDelete();

You can modify the query parameter delete before a certain date:

const doBeforeDateDelete = async () => {
  var response1 = await Model.deleteMany({createdAt:{"$lt": new Date(2022, 8, 15) }});
}

doBeforeDateDelete();
  • Related