mongodbHow to delete many documents at once in MongoDB?
MongoDB provides a deleteMany()
method to delete multiple documents from a collection. The syntax for deleteMany()
is as follows:
db.collection.deleteMany(
<filter>,
{
justOne: <boolean>,
writeConcern: <document>
}
)
<filter>
: Specifies the selection criteria to delete the documents.justOne
: Optional. To delete only one document, set totrue
. The default value isfalse
, which deletes all documents that match the criteria.writeConcern
: Optional. A document expressing the write concern.
For example, the following operation deletes all documents in the collection inventory
where the status
field equals A
:
db.inventory.deleteMany( { status : "A" } )
The output of the above example will be:
{ "acknowledged" : true, "deletedCount" : 3 }
For more information, please refer to the MongoDB documentation.
More of Mongodb
- How to check the version of MongoDB?
- How to use watch in MongoDB?
- How to update one document in MongoDB?
- How to update many documents in MongoDB?
- How to use MongoDB queue?
- How to create a many to many relation in MongoDB?
- How to insert new document into MongoDB?
- How to use hint in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
See more codes...