How to delete the last item of a collection in mongodb

Published on Author Code Father

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

Comments

comments