Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • MongoDB - Get Started
  • What is MongoDB?
  • Install MongoDB
  • MongoDB Server
  • MongoDB Shell
  • MongoDB Shell Commands
  • MongoDB Compass
  • MongoDB Database
  • MongoDB Collections
  • MongoDB Documents
  • Insert Single Document
  • Insert Multiple Documents
  • Import Data into Collection
  • Find Single Document
  • Find Multiple Documents
  • MongoDB Cursor
  • MongoDB Sort Documents
  • Update Single Document
  • Update Multiple Documents
  • Update Arrays
  • Update Embedded Documents
  • Delete Documents
  • Relations in MongoDB
  • Aggregation in MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

MongoDB: Delete Documents in a Collection

MongoDB provides the following methods to delete one or more documents in a collection.

  • db.collection.deleteOne() - Deletes the first matching document in a collection.
  • db.collection.deleteMany() - Deletes all the matching documents in a collection.

db.collection.deleteOne()

Use the db.<collection>.deleteOne() method to delete the first documents that match with the specified filter criteria in a collection.

Syntax:

db.collection.deleteOne(filter, options)

Parameters:

  1. filter: The selection criteria for the update, same as find() method.
  2. options: Optional. May contains options for update behavior. It includes writeConcern, collation, and hint parameters.

In the above syntax, db points to the current database, <collection> points is an existing collection name.

To demonstrate the delete operation, insert the following sample documents in the employees collection.

Sample Data
db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        email: "[email protected]",
        salary: 5000
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        email: "[email protected]",
        salary: 8000
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        email: "[email protected]",
        salary: 7500
    },
    { 
        _id:4,
        firstName: "Steve",
        lastName: "J",
        email: "[email protected]",
        salary: 7000
    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        email: "[email protected]",
        salary: 4500
    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        email: "[email protected]",
        salary: 7000
    }
])

The following deletes a document from the employees collection.

Example: Delete Single Document using deleteOne()
db.employees.deleteOne({ salary:7000 })
Output
{
  acknowledged: true,
  deletedCount: 1
}

The above command deletes the first matching document even if multiple documents match with the specified criteria.

deleteMany()

Use the db.<collection>.deleteMany() method to delete all the documents that match with the specified filter criteria in a collection.

Syntax:

db.collection.deleteMany(filter, options)

Parameters:

  1. filter: The selection criteria for the update, same as find() method.
  2. options: Optional. May contains options for update behavior. It includes writeConcern and collation, and hint parameters.

The following deletes all documents from the employees collection that match with the specified criteria.

Example: deleteMany()
db.employees.deleteMany({ salary:7000 })
Output
{
  acknowledged: true,
  deletedCount: 2
}

The following deletes all documents where salary is less than 7000.

Example: deleteMany()
db.employees.deleteMany({ salary: { $lt:7000} })
Output
{
  acknowledged: true,
  deletedCount: 2
}

If you specify an empty criteria then it will delete all documents in a collection.

Example: Delete All Documents
db.employees.deleteMany({ }) //deletes all documents
Output
{
  acknowledged: true,
  deletedCount: 6
}
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.