0% completed
Field update operators in MongoDB are used to modify the values of fields within documents. These operators allow you to update existing fields, add new fields, remove fields, and perform various other modifications on the documents in a collection.
Understanding these operators is essential for managing and maintaining the data in your MongoDB database.
Operator | Description |
---|---|
$set | Sets the value of a field in a document. If the field does not exist, $set creates it. |
$unset | Removes the specified field from a document. |
$inc | Increments the value of the specified field by the given amount. |
$mul | Multiplies the value of the specified field by the given amount. |
$rename | Renames a field. |
$min | Only updates the field if the specified value is less than the current value of the field. |
$max | Only updates the field if the specified value is greater than the current value of the field. |
$currentDate | Sets the value of a field to the current date. |
The general syntax for using field update operators in MongoDB is:
db.collection.updateOne( { <filter> }, { <updateOperator>: { <field>: <value> } }, <options> )
First, let's insert some documents into the users
collection to work with:
db.users.insertMany([ { name: "Alice", age: 25, scores: [85, 90, 88] }, { name: "Bob", age: 30, scores: [78, 82, 79] }, { name: "Charlie", age: 35, scores: [92, 85, 90] }, { name: "David", age: 28, scores: [88, 84, 89] }, { name: "Eve", age: 22, scores: [80, 86, 81] } ])
The $set
operator is used to set the value of a field in a document. If the field does not exist, $set
will create it.
Set Age for Alice
db.users.updateOne( { name: "Alice" }, { $set: { age: 26 } } )
This command sets the age
field to 26 for the document where the name
is "Alice". If the age
field does not exist, it will be created.
Add a New Field "city"
db.users.updateOne( { name: "Bob" }, { $set: { city: "New York" } } )
This command adds a new field city
with the value "New York" to the document where the name
is "Bob".
The $unset
operator removes the specified field from a document.
*Remove Age for Charlie:
db.users.updateOne( { name: "Charlie" }, { $unset: { age: "" } } )
This command removes the age
field from the document where the name
is "Charlie".
Remove Scores for David
db.users.updateOne( { name: "David" }, { $unset: { scores: "" } } )
This command removes the scores
field from the document where the name
is "David".
The $inc
operator increments the value of the specified field by the given amount.
Increment Age for Eve
db.users.updateOne( { name: "Eve" }, { $inc: { age: 1 } } )
This command increments the age
field by 1 for the document where the name
is "Eve". If the age
field is 22, it will become 23.
Decrement Age for Bob
db.users.updateOne( { name: "Bob" }, { $inc: { age: -1 } } )
This command decrements the age
field by 1 for the document where the name
is "Bob". If the age
field is 30, it will become 29.
The $mul
operator multiplies the value of the specified field by the given amount.
Double the Age for Alice
db.users.updateOne( { name: "Alice" }, { $mul: { age: 2 } } )
This command multiplies the age
field by 2 for the document where the name
is "Alice". If the age
field is 25, it will become 50.
Halve the Scores for Charlie
db.users.updateOne( { name: "Charlie" }, { $mul: { scores: 0.5 } } )
This command multiplies each value in the scores
array by 0.5 for the document where the name
is "Charlie". If the scores
array is [92, 85, 90], it will become [46, 42.5, 45].
The $rename
operator renames a field.
Rename Age to Years for David
db.users.updateOne( { name: "David" }, { $rename: { age: "years" } } )
This command renames the age
field to years
for the document where the name
is "David".
Rename Scores to Grades for Eve
db.users.updateOne( { name: "Eve" }, { $rename: { scores: "grades" } } )
This command renames the scores
field to grades
for the document where the name
is "Eve".
The $min
operator only updates the field if the specified value is less than the current value of the field.
Set Minimum Age for Alice
db.users.updateOne( { name: "Alice" }, { $min: { age: 24 } } )
This command sets the age
field to 24 for the document where the name
is "Alice" only if the current age
is greater than 24.
Set Minimum Score for Bob
db.users.updateOne( { name: "Bob" }, { $min: { "scores.0": 80 } } )
This command sets the first element of the scores
array to 80 for the document where the name
is "Bob" only if the current value is greater than 80.
The $max
operator only updates the field if the specified value is greater than the current value of the field.
Set Maximum Age for Charlie
db.users.updateOne( { name: "Charlie" }, { $max: { age: 40 } } )
This command sets the age
field to 40 for the document where the name
is "Charlie" only if the current age
is less than 40.
Set Maximum Score for David
db.users.updateOne( { name: "David" }, { $max: { "scores.0": 95 } } )
This command sets the first element of the scores
array to 95 for the document where the name
is "David" only if the current value is less than 95.
The $currentDate
operator sets the value of a field to the current date. This can be useful for timestamping updates or setting a field to the current date and time.
Set Last Modified Date for Alice
db.users.updateOne( { name: "Alice" }, { $currentDate: { lastModified: true } } )
This command sets the lastModified
field to the current date and time for the document where the name
is "Alice". If the lastModified
field does not exist, it will be created.
Set Last Accessed Date as Timestamp for Bob
db.users.updateOne( { name: "Bob" }, { $currentDate: { lastAccessed: { $type: "timestamp" } } } )
This command sets the lastAccessed
field to the current timestamp for the document where the name
is "Bob". The $type
option specifies that the field should be stored as a BSON timestamp.
.....
.....
.....