JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

$unset operator in MongoDB

Author:JIYIK Last Updated:2025/04/27 Views:

This article will discuss how the $unset operator works in MongoDB. Additionally, we will demonstrate the use of this operator to remove a field from all documents in a MongoDB collection.


$unset operator in MongoDB

$unset is an operator used to remove a field from an entity. It is used in the update method of MongoDB and will not have any effect if no match is found in the said method.

Suppose you have the following document in a MongoDB database:

> db.employee.find().pretty()
{"_id":1,   "name":"Alice", "salary": 1500}
{"_id":2,   "name":"Bob",   "salary": 2500}
{"_id":3,   "name":"Jhon",  "salary": 3500, "role":"Owner"}
{"_id":4,   "name":"Grace", "salary": 4500}

You have 4 entities in the document and you have used the find() method without parameters. It will return all the entities in the employee collection and pretty() is used to print the entities in a formatted manner.

Now, suppose you don't want to show the field role for some reason. So, you should remove it from the row.

The query should look like this:

> db.employee.update({_id:3}, {$unset : {"role":""}} )
> db.employee.find().pretty()
{"_id":1,   "name":"Alice", "salary": 1500}
{"_id":2,   "name":"Bob",   "salary": 2500}
{"_id":3,   "name":"Jhon",  "salary": 3500}
{"_id":4,   "name":"Grace", "salary": 4500}

Now it will remove the field role from the first matching document where id = 3.

Another option named multi is short for multiple and has a default value of false.

If true, affects update queries for all matching documents.


Use the $unset operator to remove a field from all documents in MongoDB

Suppose you are asked to delete the field salary from all documents in a MongoDB collection. If we use one update command for each id, it will take longer time.

Instead, we should do the following:

> db.employee.update({}, {$unset:{"salary": ""}}, {multi: true})
> db.employee.find().pretty()
{"_id":1,   "name":"Alice"  }
{"_id":2,   "name":"Bob"    }
{"_id":3,   "name":"Jhon"   }
{"_id":4,   "name":"Grace"  }

We used an empty object in the update query and set true in multi. Basically, {} will select all the documents in the object, while {multi: true} affects all the matching documents in the update method.

Here is the basic format (from the official documentation):

db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <update>, <options>)

In update methods you can use $unsetthe operator, and in options you can use multi.

Previous:$ne operator in MongoDB

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

$ne operator in MongoDB

Publish Date:2025/04/11 Views:84 Category:MongoDB

This article will discuss how the $ne operator works in MongoDB. In addition, we will list its differences from the $not operator. $ne operator in MongoDB $ne is an operator in MongoDB that stands for not equal to. This will compare the val

MongoDB $Set Operator

Publish Date:2025/04/11 Views:159 Category:MongoDB

With the help of this article, you will learn how to use $set the operator to partially update objects in MongoDB so that the new object overlaps/merges with the existing object. The $set operator replaces the value of a field with a given

Difference between $push and $addToSet in MongoDB

Publish Date:2025/04/11 Views:63 Category:MongoDB

This article explains the operators in MongoDB. What is the purpose of $push and $addToSet operators. Furthermore, the difference between these two operators is given in the code snippet. This article discusses the following topics. Operato

Sort a collection by date in MongoDB

Publish Date:2025/04/11 Views:64 Category:MongoDB

In this MongoDB tutorial, the problem of sorting a collection in MongoDB is discussed. The different ways to sort a collection in the database are briefly explained. Using sort() function in MongoDB This problem is solved using the MongoDB

Counting records in MongoDB

Publish Date:2025/04/11 Views:146 Category:MongoDB

This article discusses operators in MongoDB, aggregation operators, and different ways to calculate the total number of records. Operations in MongoDB CRUD operations are a user interface concept that allows users to browse, search, and cha

Pretty printing in MongoDB

Publish Date:2025/04/11 Views:150 Category:MongoDB

This article will discuss how to use pretty printing in MongoDB to display formatted results. Pretty printing in MongoDB A cursor is an object that allows programmers in the Mongo world to iterate over documents in a Mongo collection. Altho

MongoDB Adding Elements to an Array

Publish Date:2025/04/11 Views:136 Category:MongoDB

This article will cover the various ways to add to an array in MongoDB. Adding to an array in MongoDB Use the $push operator to add values ​​to an array The $push operator is one of the various array update operators provided by MongoDB

MongoDB Search by ID

Publish Date:2025/04/11 Views:131 Category:MongoDB

The following article provides an overview of MongoDB find by Id() method. MongoDB provides a find by Id() function which can retrieve documents matching a user id. To use search by Id() in MongoDB, you need to use the find() function. If n

MongoDB starts with a query

Publish Date:2025/04/10 Views:196 Category:MongoDB

In this MongoDB article, users will learn how to start queries using $regex. It provides regular expression functionality for pattern matching strings in queries. MongoDB starts querying using $regex If you want to use $regex , use one of t

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial