How to Remove a Field Completely From a MongoDB Document?
Last Updated :
26 Mar, 2024
In MongoDB, managing data is flexible and efficient, allowing developers to adapt to evolving application requirements easily. One common operation is removing fields from documents.
Whether it's to Organize our data structure, improve performance or change business needs, MongoDB provides straightforward methods to achieve this task.
In this article, we'll explore how to remove a field completely from a MongoDB document with the help of an example.
How to Remove a Field from a MongoDB Document?
When we no longer need a field in a MongoDB document, The problem involves removing specific fields from MongoDB documents. By targeting the documents and fields to be removed, developers can effectively clean up unnecessary data from their MongoDB collections. This can be achieved using the below method which removes the specified fields from documents.
- Using $unset Operator
- Using updateMany Operator
- Using options {multi: true} instead of updateMany
Let's set up an environment
Suppose we have a Gfg database in which an Employee collection. with some documents that contain details about employees in the form of field-value pairs.

1. Using $unset Operator
The $unset operator is used to remove a specific field from a MongoDB document. It takes the field name as its argument and removes that field completely from the document. This method is useful when you want to remove a single field from a document.
Example: Let say, we want to remove the department field from an employee document with a specific _id.
db.Employee.update
(
{ _id: ObjectId("65f8b8499abfc382ebcd9668") },
{ $unset: { department: "" } }
)
Output:
OutputExplanation:
- The update operation targets the document with the specified _id.
- The $unset operator removes the department field completely from the document.
- Upon successful execution, the department field will be removed from the targeted employee document.
2. Using updateMany Operator
The updateMany operator is used to update multiple documents in a collection that match a specified filter. When combined with the $unset operator, it can be used to remove a field from multiple documents at once.
Example: Suppose we want to remove the personalDetails contactInfo from all documents of a Collection Employee.
There are two ways to remove the field from documents. But the result will be same.
db.Employee.updateMany(
{},
{ $unset: { "personalDetails.contactInfo": "" } }
)
Output:
OutputExplanation:
- The updateMany operation target the documents with the specified given condition.
- The $unset operator removes the personalDetails's contactInfo field completely from documents.
- Upon successful execution, the personalDetails's contactInfo field will be removed from the targeted employee documents.
3. Using Options {multi: true} Instead of upadateMany
The {multi: true} option is used with the update method to update multiple documents in a collection that match a specified filter. This method is an alternative to using updateMany and provides more flexibility in specifying the update criteria.
It can be combined with the $unset operator to remove multiple fields from multiple documents at once.
Example: Our task is to remove the "contactInfo" and "joiningDate" fields from all documents in the "Employee" collection in MongoDB using a single update operation with the $unset operator and {multi: true} option.
db.Employee.update(
{},
{ $unset: { "personalDetails.contactInfo": "", "joiningDate": "" } },
{ multi: true }
);
Output:
OutputExplanation:
- Instead of updateMany, update and {multi:true} as option is used for targeting the documents with the specified given condition.
- The $unset operator removes the personalDetails's contactInfo field completely from documents. Upon successful execution, the personalDetails's contactInfo field will be removed from the targeted employee documents.
- We can also remove multiple field by passing the multiple field in the $unset.
Conclusion
Overall, removing a field completely from a document is a straightforward process. By using the $unset
operator with the update()
or updateMany()
method and with {multi:true}, you can efficiently remove unwanted fields from your documents. This helps maintain a clean and efficient database structure, ensuring that your data remains organized and easy to manage.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15+ min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read