Search⌘ K

Bulk Write: Part 2

Explore how to perform bulk write operations in MongoDB including deleting single or multiple documents with deleteOne and deleteMany, replacing documents with replaceOne, and using upsert options. Understand the response structure of these operations and how to optimize bulk writes with options like writeConcern and ordered.

Bulk write operations

deleteOne

The deleteOne operation is used to delete one document.

Syntax:

db.<collection-name>.bulkWrite([
   { 
       deleteOne : { 
           "filter" : <document>,
           "collation" : <document> 
        } 
    }
])

This function deletes the first document in the collection that matches the "filter" : <documents>.

Below is an example of how to perform delete operations on the task collection using bulkWrite.

Markdown
db.tasks.bulkWrite([
{
deleteOne : {
filter : {
name: "Task 1"
},
}
}
])

This query returns the below output.

{
  acknowledged: true,
  insertedCount: 0,
  insertedIds: {},
  matchedCount: 0,
  modifiedCount: 0,
  **deletedCount: 1,**
  upsertedCount: 0,
  upsertedIds: {}
}

Here, the deleteCount:1 means that ...