I'm new to REST apis and want to simply delete the last POST in my mongoDB collection using a DELETE route with mongoose. The other answers on here do not explain how to do this via a route.. only using the db object.
This is what my current route looks like (I'm getting no errors but it's not deleting):
//DELETE LAST POST
router.delete('/last', async (req,res) => {
try {
const removedPost = await Post.findAndModify({query :{},"sort": { "_id": -1 }, remove:true})
res.json(removedPost)
}catch(err){
res.json({ message: err })
}
})
I've also tried doing it with findOneAndDelete()
but it's the same, no errors in the logs, 200 OK response, no posts delete:
const removedPost = await Post.findOneAndDelete(
{ __v: 0 },
{ sort: { "date": -1 } }
);
I've also tried it like this without the optional 'projection' as i understand it from the docs:
const removedPost = await Post.findOneAndDelete(
{},
{ sort: { "date": -1 } }
);
I'm calling this route in postman: https://ag-sheets-api.herokuapp.com/posts/last, without any body. And it's response is 200 OK and:
{
"message": {
"stringValue": ""last"",
"kind": "ObjectId",
"value": "last",
"path": "_id",
"reason": {}
}
}
This is what one of the posts looks like:
_id:6012f937a892270017007b31
title:"97273282974158320"
date:2021-01-28T17:49:43.582+00:00
__v:0
en1:"<div class="contact_us"><p style="font-size: 16px; font-weight: bold; ..."
en2:"<div class="qw"><div class="qe" data-country="cn.svg"><p class="qr">Ch..."
en3:"<div class="qw"><div class="qe" data-country="iq.svg"><p class="qr">Ir..."
en4:"<div class="qw"><div class="qe" data-country="pe.svg"><p class="qr">Pe..."
en5:"<div class="qw"><div class="qe" data-country="ta.svg"><p class="qr">Tr..."
fr1:"<div class="contact_us"><p style="font-size: 16px; font-weight: bold; ..."
fr2:"<div class="qw"><div class="qe" data-country="cn.svg"><p class="qr">Ch..."
fr3:"<div class="qw"><div class="qe" data-country="iq.svg"><p class="qr">Ir..."
fr4:"<div class="qw"><div class="qe" data-country="pe.svg"><p class="qr">Pe..."
fr5:"<div class="qw"><div class="qe" data-country="ta.svg"><p class="qr">Tr..."
de1:"<div class="contact_us"><p style="font-size: 16px; font-weight: bold; ..."
de2:"<div class="qw"><div class="qe" data-country="cn.svg"><p class="qr">Ch..."
de3:"<div class="qw"><div class="qe" data-country="iq.svg"><p class="qr">Ir..."
de4:"<div class="qw"><div class="qe" data-country="pe.svg"><p class="qr">Pe..."
de5:"<div class="qw"><div class="qe" data-country="ta.svg"><p class="qr">Tr..."
Most stackoverflow answers to this question say something like this below, but I don't understand how that can be achieved inside a mongoose DELETE route, after all there is no db object is there? Where are they doing this?:
db.coll.find().sort({_id:-1}).limit(100);
It looks like using limit on the standard mongo remove operation isn't supported though, so you might use something like this to delete the 100 documents:
for(i=0;i<100;i++) {
db.coll.findAndModify({query :{}, sort: {"_id" : -1}, remove:true})
}
question from:
https://stackoverflow.com/questions/65944047/delete-last-document-in-mongodb-collection-with-delete-request