How to Use the MongoDB $max Operator to Update Field Values

Summary: in this tutorial, you’ll learn how to use the MongoDB $max operator to update field values to specified values.

Introduction to the MongoDB $max operator

The $max is a field update operator that allows you to update the value of a field to a specified value if the specified value is greater than (>) the current value of the field.

Here is the syntax of the $max operator:

{ $max: {<field1>: <value1>, ...} }
Code language: HTML, XML (xml)

If the current value of a field is less than or equal to the value that you want to update, the $max operator won’t update the value.

If the field doesn’t exist, the $max operator creates the field and sets its value to the specified value.

MongoDB $max operator example

We’ll use the following products collection:

db.products.insertMany([ { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]}, { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]}, { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]}, { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]}, { "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]} ])
Code language: JavaScript (javascript)

The following example uses the $max operator to update the price of the document _id 1:

db.products.updateOne({ _id: 1 }, { $max: { price: 699 } })
Code language: PHP (php)

The query found one matching document but it didn’t any document as shown in the modifiedCount value:

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 }
Code language: JSON / JSON with Comments (json)

The reason is that the new value that we want to update is 699 which is less than the current value of the price field 799.

The following example uses the $min operator to update the price of the document _id 1 to 899:

db.products.updateOne({ _id: 1 }, { $max: { price: 899 } })
Code language: PHP (php)

In this case, the price field of the document _id 1 is updated to 899:

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
Code language: JSON / JSON with Comments (json)

This query verifies the update:

db.products.find({ _id: 1 }, { name: 1, price: 1 })
Code language: CSS (css)

Output:

{ "_id" : 1, "name" : "xPhone", "price" : 899 }
Code language: JSON / JSON with Comments (json)

Summary

  • Use the $max operator to update the value of a field to a specified value when the specified value is greater than the current value.
Was this tutorial helpful ?