[英]Delete last document 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.我是 REST api 的新手,只想使用带有 mongoose 的 DELETE 路由删除我的 mongoDB 集合中的最后一个 POST。这里的其他答案没有解释如何通过路由执行此操作。仅使用 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:我也试过用
findOneAndDelete()
来做,但它是一样的,日志中没有错误,200 OK 响应,没有删除帖子:
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.我在 postman 中调用这条路线: https://ag-sheets-api.herokuapp.com/posts/last ,没有任何身体。 And it's response is 200 OK and:
它的响应是 200 OK 并且:
{
"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?这个问题的大多数 stackoverflow 答案都在下面这样说,但我不明白如何在 mongoose DELETE 路由中实现,毕竟没有 db object 吗? 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})
}
you can try like this:你可以这样尝试:
let x = await Post.findOne({}).sort({date: -1});
await x.remove();
so your route should be like this:所以你的路线应该是这样的:
router.delete("/", async (req, res) => {
try {
let x = await Post.findOne({}).sort({ date: -1 });
await x.remove();
res.status(200).json({ message: "deleted place" })
} catch (err) {
res.json({ message: err });
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.