Learning MongoDB

0% completed

Previous
Next
Field Update Operators

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.

Field Update Operators

OperatorDescription
$setSets the value of a field in a document. If the field does not exist, $set creates it.
$unsetRemoves the specified field from a document.
$incIncrements the value of the specified field by the given amount.
$mulMultiplies the value of the specified field by the given amount.
$renameRenames a field.
$minOnly updates the field if the specified value is less than the current value of the field.
$maxOnly updates the field if the specified value is greater than the current value of the field.
$currentDateSets the value of a field to the current date.

Syntax for Using Field Update Operators

The general syntax for using field update operators in MongoDB is:

db.collection.updateOne( { <filter> }, { <updateOperator>: { <field>: <value> } }, <options> )
  • filter: The criteria to match documents for the update.
  • updateOperator: The update operator to apply.
  • field: The field to update.
  • value: The new value for the field.
  • options (optional): Additional options for the update operation.

Example Setup

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] } ])

$set Operator

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.

Examples

  1. 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.

  2. 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".

$unset Operator

The $unset operator removes the specified field from a document.

Examples

  1. *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".

  2. 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".

$inc Operator

The $inc operator increments the value of the specified field by the given amount.

Examples

  1. 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.

  2. 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.

$mul Operator

The $mul operator multiplies the value of the specified field by the given amount.

Examples

  1. 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.

  2. 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].

$rename Operator

The $rename operator renames a field.

Examples

  1. 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".

  2. 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".

$min Operator

The $min operator only updates the field if the specified value is less than the current value of the field.

Examples

  1. 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.

  2. 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.

$max Operator

The $max operator only updates the field if the specified value is greater than the current value of the field.

Examples

  1. 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.

  2. 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.

$currentDate Operator

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.

Examples

  1. 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.

  2. 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.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next