如何在mongodb中删除集合的最后一项 [英] How to delete the last item of a collection in mongodb

查看:70
本文介绍了如何在mongodb中删除集合的最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用python和mongodb编写了一个程序来写一些日记.像这样

I made a program with python and mongodb to do some diaries. Like this

有时我想删除最后一句话,只需输入删除!"即可.但我不知道如何以Samrt方式删除.我不想使用跳过".有什么好办法吗?

Sometimes I want to delete the last sentence, just by typing "delete!" But I dont know how to delete in a samrt way. I dont want to use "skip". Is there a good way to do it?

推荐答案

无论是第一项还是最后一项, MongoDB 都会为每条记录维护唯一的 _id 键,因此您可以只需使用 deleteOne() deleteMany()在删除查询中传递该id字段即可.由于只有一条记录要删除,因此您需要像

那样使用 deleteOne()

Be it first or last item, MongoDB maintains unique _id key for each record and thus you can just pass that id field in your delete query either using deleteOne() or deleteMany(). Since only one record to delete you need to use deleteOne() like

db.collection_name.deleteOne({"_id": "1234"}) // replace 1234 with actual id

这篇关于如何在mongodb中删除集合的最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆