Delete last record in MongoDB collection with DELETE request

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})
}

34 thoughts on “Delete last record in MongoDB collection with DELETE request”

  1. 939809 145876Whoah this blog is magnificent i truly like reading your articles. Keep up the excellent paintings! You realize, lots of persons are searching round for this details, you can aid them greatly. 384070

    Reply
  2. Heya i am for the primary time here. I came across
    this board and I to find It truly useful & it helped me out much.
    I hope to present one thing back and aid others like you helped me.

    Reply
  3. Thank you, I’ve just been looking for information about this topic for ages and yours is the
    best I have discovered so far. But, what about the conclusion? Are you sure
    about the supply?

    Reply

Leave a Comment