MongoDB (8)-Document Delete Operation

Delete method

Delete a single document
Delete single or multiple documents

deleteOne() syntax format

db.collection.deleteOne(   <filter>,   {      writeConcern: <document>,      collation: <document>,      hint: <document|string>        // Available starting in MongoDB 4.4   })
  • filter: The condition for deleting the document, a bit like the where condition of Mysql
  • hint: Specify the index of the query

deleteMany() syntax format

db.collection.deleteMany(   <filter>,   {      writeConcern: <document>,      collation: <document>   })

remove() syntax format

Grammar One

db.collection.remove(   <query>,   <justOne>)

Grammar Two

db.collection.remove(   <query>,   {     justOne: <boolean>,     writeConcern: <document>,     collation: <document>   })

Parameter Description

  • query/filter: Use query operators to specify deletion conditions. To delete all documents in the collection, pass an empty document ({})
  • justOne: optional, true, delete the first document that meets the delete condition, default false, delete all documents that meet the condition
  • writeConcern: Optional, set the level of throwing exceptions (more complicated, we will explain in detail when we use it)
  • collation: Specify the collation used for the operation (complex, and then explain it in detail)


Deleted documents are permanently deleted and cannot be undone or restored

Insert data

db.inventory.insertMany( [   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },] );

For the chestnuts behind

Delete all documents


What if deleteOne() only passes {}?


Only delete the first document in the collection

Delete documents with specified conditions

db.inventory.deleteMany({ status : "A" })

Equivalent writing


What about deleteOne()?


There are two documents with status "A", but only the first one that matches will be deleted

Conditional deletion

Delete documents whose qty field value is greater than 50

db.inventory.remove( { qty: { $gt: 50 } } 

Delete the first document that matches h greater than 8.5

db.inventory.remove( { "size.h" : { $gt : 8.5 } } , { justOne : true } )

Key points of the delete operation

  • Deleting a document does not delete the index
  • All operations are atomic at the level of a single document

The following methods can also delete documents from the collection





More chestnuts will be used later to supplement