MongoDB Updating Operators
Updating Operators in MongoDB
Update operators in MongoDB are used to modify the content of documents in a collection. These operators allow you to update existing fields, add new fields, remove fields, and work with arrays. MongoDB’s update operations, combined with these operators, give you flexibility in modifying document data.
Here’s an overview of the most common update operators in MongoDB:
1. $set
The $set
operator updates the value of a field in a document. If the field does not exist, MongoDB will add it.
Syntax:
db.collection.updateMany(
{ filter },
{ $set: { "field": value } }
)
Example:
Set the status
field to "active"
:
db.users.updateMany(
{ },
{ $set: { "status": "active" } }
)
2. $unset
The $unset
operator removes the specified field from a document.
Syntax:
db.collection.updateMany(
{ filter },
{ $unset: { "field": "" } }
)
Example:
Remove the temporaryField
from documents:
db.users.updateMany(
{ },
{ $unset: { "temporaryField": "" } }
)
3. $inc
The $inc
operator increments the value of a field by a specified amount. It is commonly used with numerical fields.
Syntax:
db.collection.updateMany(
{ filter },
{ $inc: { "field": amount } }
)
Example:
Increment the views
field by 1:
db.posts.updateMany(
{ },
{ $inc: { "views": 1 } }
)
4. $mul
The $mul
operator multiplies the value of a field by a specified factor.
Syntax:
db.collection.updateMany(
{ filter },
{ $mul: { "field": multiplier } }
)
Example:
Multiply the price
field by 1.1 to apply a 10% price increase:
db.products.updateMany(
{ },
{ $mul: { "price": 1.1 } }
)
5. $rename
The $rename
operator renames a field in a document.
Syntax:
db.collection.updateMany(
{ filter },
{ $rename: { "oldFieldName": "newFieldName" } }
)
Example:
Rename the field oldName
to newName
:
db.users.updateMany(
{ },
{ $rename: { "oldName": "newName" } }
)
6. $min
The $min
operator updates the field to a new value if the new value is less than the current field value.
Syntax:
db.collection.updateMany(
{ filter },
{ $min: { "field": value } }
)
Example:
Set the price
field to a lower value, if the new value is less:
db.products.updateMany(
{ },
{ $min: { "price": 50 } }
)
7. $max
The $max
operator updates the field to a new value if the new value is greater than the current field value.
Syntax:
db.collection.updateMany(
{ filter },
{ $max: { "field": value } }
)
Example:
Set the price
field to a higher value, if the new value is greater:
db.products.updateMany(
{ },
{ $max: { "price": 200 } }
)
8. $currentDate
The $currentDate
operator sets a field’s value to the current date and time. You can store either a date or a timestamp.
Syntax:
db.collection.updateMany(
{ filter },
{ $currentDate: { "field": { $type: "timestamp" or "date" } } }
)
Example:
Update the lastUpdated
field to the current date:
db.posts.updateMany(
{ },
{ $currentDate: { "lastUpdated": true } }
)
You can also specify that you want to store a timestamp:
db.posts.updateMany(
{ },
{ $currentDate: { "lastUpdated": { $type: "timestamp" } } }
)
9. $addToSet
The $addToSet
operator adds a value to an array field if the value does not already exist in the array.
Syntax:
db.collection.updateMany(
{ filter },
{ $addToSet: { "arrayField": value } }
)
Example:
Add a tag to the tags
array only if it doesn’t already exist:
db.posts.updateMany(
{ },
{ $addToSet: { "tags": "mongodb" } }
)
10. $push
The $push
operator adds a value to an array field, even if it already exists.
Syntax:
db.collection.updateMany(
{ filter },
{ $push: { "arrayField": value } }
)
Example:
Add a new item to the comments
array:
db.posts.updateMany(
{ },
{ $push: { "comments": { user: "John", comment: "Great post!" } } }
)
11. $pop
The $pop
operator removes the first or last element from an array.
- Use
-1
to remove the first element. - Use
1
to remove the last element.
Syntax:
db.collection.updateMany(
{ filter },
{ $pop: { "arrayField": 1 or -1 } }
)
Example:
Remove the last element from the comments
array:
db.posts.updateMany(
{ },
{ $pop: { "comments": 1 } }
)
12. $pull
The $pull
operator removes all array elements that match a specified condition.
Syntax:
db.collection.updateMany(
{ filter },
{ $pull: { "arrayField": { condition } } }
)
Example:
Remove all comments from the comments
array where the username is "John"
:
db.posts.updateMany(
{ },
{ $pull: { "comments": { user: "John" } } }
)
13. $pullAll
The $pullAll
operator removes all instances of the specified values from an array.
Syntax:
db.collection.updateMany(
{ filter },
{ $pullAll: { "arrayField": [value1, value2] } }
)
Example:
Remove the values "mongodb"
and "nodejs"
from the tags
array:
db.posts.updateMany(
{ },
{ $pullAll: { "tags": ["mongodb", "nodejs"] } }
)
14. $bit
The $bit
operator performs bitwise operations on integer fields. It supports the following operations:
- and
- or
- xor
Syntax:
db.collection.updateMany(
{ filter },
{ $bit: { "field": { and: value or or: value or xor: value } } }
)
Example:
Perform a bitwise AND on the flags
field:
db.items.updateMany(
{ },
{ $bit: { "flags": { and: 5 } } }
)