MongoDB Update Operators
Modify documents efficiently with update operators
✏️ What are Update Operators?
Update operators modify existing documents in MongoDB. They let you change field values, add new fields, remove fields, or update arrays without replacing the entire document.
// Increase product price by 10
db.products.updateOne(
{ _id: 1 },
{ $inc: { price: 10 } }
)
Field Update Operators
Field operators modify specific fields in documents. Use them to set values, increment numbers, rename fields, or remove fields entirely.
$set
Sets the value of a field
db.users.updateOne(
{ name: "John" },
{ $set: { age: 30 } }
)
$unset
Removes a field from document
db.users.updateOne(
{ name: "John" },
{ $unset: { age: "" } }
)
$inc
Increments field by specified value
db.products.updateOne(
{ _id: 1 },
{ $inc: { views: 1 } }
)
$mul
Multiplies field by specified value
db.products.updateOne(
{ _id: 1 },
{ $mul: { price: 1.1 } }
)
🔹 $set Operator
The $set operator replaces the value of a field with the specified value. If the field doesn't exist, it creates it.
🔸 Basic Usage
// Update single field
db.users.updateOne(
{ name: "Alice" },
{ $set: { email: "[email protected]" } }
)
// Update multiple fields
db.users.updateOne(
{ name: "Bob" },
{
$set: {
email: "[email protected]",
age: 28,
city: "New York"
}
}
)
Before:
{ "_id": 1, "name": "Bob", "age": 25 }
After:
{ "_id": 1, "name": "Bob", "age": 28, "email": "[email protected]", "city": "New York" }
🔹 $inc Operator
The $inc operator increments a numeric field by a specified value. Use negative numbers to decrement.
🔸 Increment Examples
// Increase product stock by 50
db.products.updateOne(
{ _id: 1 },
{ $inc: { stock: 50 } }
)
// Decrease price by 5 (negative increment)
db.products.updateOne(
{ _id: 1 },
{ $inc: { price: -5 } }
)
// Increment multiple fields
db.posts.updateOne(
{ _id: 1 },
{
$inc: {
views: 1,
likes: 1
}
}
)
Before:
{ "_id": 1, "title": "My Post", "views": 100, "likes": 5 }
After:
{ "_id": 1, "title": "My Post", "views": 101, "likes": 6 }
🔹 Array Update Operators
Array operators add, remove, or modify elements in array fields without replacing the entire array.
🔸 $push Operator
// Add item to array
db.users.updateOne(
{ name: "John" },
{ $push: { hobbies: "reading" } }
)
// Add multiple items
db.users.updateOne(
{ name: "John" },
{ $push: { hobbies: { $each: ["gaming", "cooking"] } } }
)
Before:
{ "_id": 1, "name": "John", "hobbies": ["sports"] }
After:
{ "_id": 1, "name": "John", "hobbies": ["sports", "reading", "gaming", "cooking"] }
🔸 $pull Operator
// Remove specific value from array
db.users.updateOne(
{ name: "John" },
{ $pull: { hobbies: "gaming" } }
)
// Remove multiple values
db.products.updateOne(
{ _id: 1 },
{ $pull: { tags: { $in: ["old", "deprecated"] } } }
)
🔸 $pop Operator
// Remove last element (-1 for first)
db.users.updateOne(
{ name: "John" },
{ $pop: { hobbies: 1 } }
)
// Remove first element
db.users.updateOne(
{ name: "John" },
{ $pop: { hobbies: -1 } }
)
🔹 $addToSet Operator
The $addToSet operator adds a value to an array only if it doesn't already exist, preventing duplicates.
// Add unique tag
db.posts.updateOne(
{ _id: 1 },
{ $addToSet: { tags: "mongodb" } }
)
// Add multiple unique values
db.posts.updateOne(
{ _id: 1 },
{
$addToSet: {
tags: { $each: ["database", "nosql", "mongodb"] }
}
}
)
Before:
{ "_id": 1, "title": "Post", "tags": ["mongodb", "database"] }
After (mongodb already exists, nosql added):
{ "_id": 1, "title": "Post", "tags": ["mongodb", "database", "nosql"] }
🔹 $rename Operator
The $rename operator changes the name of a field in documents.
// Rename single field
db.users.updateMany(
{},
{ $rename: { "phone": "phoneNumber" } }
)
// Rename multiple fields
db.products.updateMany(
{},
{
$rename: {
"desc": "description",
"qty": "quantity"
}
}
)
Before:
{ "_id": 1, "name": "Product", "desc": "Great item", "qty": 100 }
After:
{ "_id": 1, "name": "Product", "description": "Great item", "quantity": 100 }
🔹 Practical Examples
Combine operators for complex updates:
Example 1: Update Product Inventory
// Decrease stock, increase sold count, update status
db.products.updateOne(
{ _id: 1 },
{
$inc: { stock: -1, soldCount: 1 },
$set: { lastSold: new Date() },
$push: {
salesHistory: {
date: new Date(),
quantity: 1
}
}
}
)
Example 2: Update User Profile
// Update profile and add new skill
db.users.updateOne(
{ email: "[email protected]" },
{
$set: {
lastLogin: new Date(),
status: "active"
},
$addToSet: { skills: "MongoDB" },
$inc: { loginCount: 1 }
}
)