In this tutorial, we will focus on how to delete documents in MongoDB. MongoDB is a popular NoSQL database that stores data in BSON format, which is a binary representation of JSON-like documents. Deleting documents is a crucial part of data management, and MongoDB provides two primary methods for this purpose: deleteOne()
and deleteMany()
.
By the end of this tutorial, you will learn:
deleteOne()
method to delete a single document.deleteMany()
method to delete multiple documents.There are no strict prerequisites for this tutorial, but it would be helpful if you have:
In MongoDB, the deleteOne()
function is used to delete the first document that matches the deletion criteria, while deleteMany()
is used to delete all documents that match the deletion criteria.
2.1 deleteOne()
The deleteOne()
function deletes the first document that matches a specified filter. Here's how it's used:
db.collection.deleteOne(query)
The query
parameter is a filter that specifies the document to delete.
2.2 deleteMany()
The deleteMany()
function deletes all documents that match a specified filter. Here's how it's used:
db.collection.deleteMany(query)
The query
parameter is a filter that specifies the documents to delete.
Example 1: Using deleteOne()
In the following example, we'll delete a single document from the employees
collection where name
is 'John Doe':
// This is your MongoDB client
const MongoClient = require('mongodb').MongoClient;
// This is your connection string
const url = 'mongodb://localhost:27017/';
// Connect to the server
MongoClient.connect(url, function(err, db) {
if (err) throw err;
// This is your database
const dbo = db.db('mydb');
// This is your query
const myquery = { name: 'John Doe' };
// This is where you're deleting one document
dbo.collection('employees').deleteOne(myquery, function(err, obj) {
if (err) throw err;
console.log('1 document deleted');
db.close();
});
});
Example 2: Using deleteMany()
In the following example, we'll delete all documents from the employees
collection where name
starts with the letter 'A':
// This is your MongoDB client
const MongoClient = require('mongodb').MongoClient;
// This is your connection string
const url = 'mongodb://localhost:27017/';
// Connect to the server
MongoClient.connect(url, function(err, db) {
if (err) throw err;
// This is your database
const dbo = db.db('mydb');
// This is your query
const myquery = { name: /^A/ };
// This is where you're deleting many documents
dbo.collection('employees').deleteMany(myquery, function(err, obj) {
if (err) throw err;
console.log(obj.result.n + ' document(s) deleted');
db.close();
});
});
In this tutorial, we covered how to delete documents in MongoDB using deleteOne()
and deleteMany()
. We also walked through examples of how to use these methods.
Next, you might want to learn about other CRUD operations (Create, Update, Read) in MongoDB.
Here are a few additional resources:
Exercise 1: Delete a document from the students
collection where age
is less than 20.
Exercise 2: Delete all documents from the employees
collection where salary
is greater than 50000.
Exercise 3: Delete all documents from the products
collection where name
starts with the letter 'B'.
Practice is key to mastering any concept. Try to solve these exercises, and don't forget to check the MongoDB documentation if you're stuck. Happy learning!