Node.js Tutorial

Node.js MongoDB Remove – Full Stack Web Project

Node.js MongoDB Remove
Node.js MongoDB Remove

Node.js MongoDB Remove

In MongoDB, you can delete documents from a collection using methods such as deleteOne() and deleteMany(). When working with Node.js and MongoDB, deleting records that are no longer required is a common database operation.

Earlier MongoDB examples commonly used the remove() method. However, remove() is deprecated in modern MongoDB applications. It is recommended to use deleteOne() when deleting a single document or deleteMany() when deleting multiple documents.

Understanding MongoDB Delete Methods

MongoDB provides different methods for removing documents from a collection. The method you choose depends on whether you want to delete one document or multiple matching documents.

MethodPurpose
deleteOne()Deletes the first document that matches the query.
deleteMany()Deletes all documents that match the query.
remove()Older MongoDB method that has been deprecated.

Creating a Query

The query object specifies which document should be deleted. For example, if you want to delete an employee whose address is Ghaziabad, you can create the following query:

var myquery = { address: "Ghaziabad" };

This query searches the employees collection for a document where the address field has the value Ghaziabad.

Example: Delete a Record Using Node.js and MongoDB

Let’s look at a practical example of deleting a single document from a MongoDB collection using Node.js.

Step 1: Create a JavaScript File

Create a file named remove.js and add the following code:

var MongoClient = require("mongodb").MongoClient;

var url = "mongodb://localhost:27017/MongoDatabase";

MongoClient.connect(url, function(err, db) {
    if (err) throw err;

    var myquery = { address: "Ghaziabad" };

    db.collection("employees").deleteOne(myquery, function(err, result) {
        if (err) throw err;

        console.log(result.deletedCount + " record(s) deleted");

        db.close();
    });
});

In this example:

  • MongoClient is used to connect Node.js with MongoDB.
  • The myquery object specifies the document to delete.
  • deleteOne() removes the first document matching the query.
  • deletedCount indicates how many documents were deleted.
  • db.close() closes the MongoDB connection.

YT:- DecodeIT

Step 2: Run the Node.js File

After saving the file, open your command terminal in the project directory and run:

node remove.js

If the connection and query are successful, the terminal will display the number of deleted records.

1 record(s) deleted

Step 3: Verify the Deletion

You can verify the deletion by checking the employees collection in MongoDB. The document whose address was Ghaziabad should no longer be present.

Delete Multiple Documents in MongoDB with Node.js

If you want to remove every document that matches a specific condition, use the deleteMany() method instead of deleteOne().

For example, the following query deletes all employees whose address is Ghaziabad:

var myquery = { address: "Ghaziabad" };

db.collection("employees").deleteMany(myquery, function(err, result) {
    if (err) throw err;

    console.log(result.deletedCount + " record(s) deleted");
});

Unlike deleteOne(), deleteMany() removes all documents that satisfy the specified condition.

More:- UPDATEGADH

Conclusion

Deleting documents is an important part of MongoDB CRUD operations. In Node.js applications, deleteOne() can be used when you need to remove a single matching document, while deleteMany() is useful for removing multiple matching documents.

Although older MongoDB examples may use the remove() method, modern applications should use deleteOne() and deleteMany() for document deletion.

SEO Details

SEO Title: Node.js MongoDB Remove: Delete Documents with Node.js

Meta Description: Learn how to remove and delete MongoDB documents using Node.js. Understand deleteOne(), deleteMany(), queries, examples, and how to verify deleted records.

Keywords: Node.js MongoDB remove, Node.js MongoDB delete, MongoDB deleteOne Node.js, MongoDB deleteMany Node.js, MongoDB delete document, MongoDB delete by ID, Node.js MongoDB CRUD, MongoDB Node.js example, MongoDB remove document, Node.js MongoDB tutorial

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us