$max Operator in MongoDB: Quick Guide

In this beginner’s guide, I will explain how you can use the $max operator.

MongoDB offers a wide range of operators that are suitable for a wide variety of uses. With MongoDB-backed applications, developers can do much more than just CRUD because MongoDB provides easy-to-understand, highly advanced, and flexible operators. There is no doubt that this is one of the factors contributing to MongoDB’s popularity.

Thanks to operators, you can now customize querying, printing, updating, and do much more with MongoDB. Regardless of how experienced the user is, this data storage platform appeals to beginners as well as seasoned developers.

Well, I will discuss the $max operator in MongoDB today since it is one of the most used and quite important operators offered by this database management system.

The $max operator allows users to update a field’s value with the given value if its value is greater than the specified value. In case the given field does not exist, then the $max operator will create a new field and set the given value. Lastly, it is important to note that the $max operator has been deprecated since the MongoDB 3.2 version release.

In this guide, I will demonstrate examples to explain how you can put to use the $max operator in MongoDB. We cannot use the $max operator with the find() method in the mongo shell.

Using the $max Operator in MongoDB

We will see two different approaches of using the $max operator to understand its behavior. First, we will use the $max operator values more than the fields’ values. Second, we will use the operator’s values lesser than the fields’ value to see how they work.

So let us get started.

Setting $max Operator Value More than Field Value

  • Start your MongoDB database server
  • Select a database you want to choose and use it:
show dbs 
use droneShop
  • Now, to start off, let us see all our drones that have “Recreation” as their utility, that we have stored in an array. We’ll use the pretty print function here.
> db.drones.find( { utility: [ "Recreation" ] } ).pretty()

{
        "_id" : ObjectId("6157239d341d653bccaf7822"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "PX - Jetfire Darkula OG",
        "price" : 10000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "JS - Wolf Hugo",
        "price" : 7800,
        "weight" : "500 grams",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "TZ - Shark 78",
        "price" : 45000,
        "weight" : "990 grams",
        "__v" : 0
}
{
        "_id" : ObjectId("6159919eebf45d32b85bd1a5"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "VF - Humphrey Disfruto",
        "price" : 6700,
        "weight" : "1.2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "LJ - Minorr Jet 62",
        "price" : 12000,
        "weight" : "2.7 kilograms",
        "__v" : 0
}
  • Now, suppose we decided to change the prices of all these drones to 60000. Well, as you can see, all our documents have a price less than 60000. Let us use the $max operator here to change the “price”.
> db.drones.updateMany( { utility: [ "Recreation" ] }, { $max: { price: 60000 } } )

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }
  • Let us now take a look at our documents:
> db.drones.find( { utility: [ "Recreation" ] } ).pretty()

{
        "_id" : ObjectId("6157239d341d653bccaf7822"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "PX - Jetfire Darkula OG",
        "price" : 60000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "JS - Wolf Hugo",
        "price" : 60000,
        "weight" : "500 grams",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "TZ - Shark 78",
        "price" : 60000,
        "weight" : "990 grams",
        "__v" : 0
}
{
        "_id" : ObjectId("6159919eebf45d32b85bd1a5"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "VF - Humphrey Disfruto",
        "price" : 60000,
        "weight" : "1.2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "LJ - Minorr Jet 62",
        "price" : 60000,
        "weight" : "2.7 kilograms",
        "__v" : 0
}

Setting $max Operator Value Less than Field Value

Let us now check the behavior of the $max operator by setting its value lesser than the “price” fields of all our documents.

> db.drones.updateMany( { utility: [ "Recreation" ] }, { $max: { price: 6000 } } )

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 0 }

You must have noticed the “modifiedCount” key equals 0; which is an indication that the operator couldn’t change anything. However, let us check our documents once again:

> db.drones.find( { utility: [ "Recreation" ] } ).pretty()

{
        "_id" : ObjectId("6157239d341d653bccaf7822"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "PX - Jetfire Darkula OG",
        "price" : 60000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "JS - Wolf Hugo",
        "price" : 60000,
        "weight" : "500 grams",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "TZ - Shark 78",
        "price" : 60000,
        "weight" : "990 grams",
        "__v" : 0
}
{
        "_id" : ObjectId("6159919eebf45d32b85bd1a5"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "VF - Humphrey Disfruto",
        "price" : 60000,
        "weight" : "1.2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Recreation"
        ],
        "onSale" : false,
        "name" : "LJ - Minorr Jet 62",
        "price" : 60000,
        "weight" : "2.7 kilograms",
        "__v" : 0
}

This happens because the $max operator doesn’t change the values of the fields that are more than the given value. This way we have successfully learned how to use the $max operator in MongoDB.

Read More: Ultimate Guide to Using addToSet Operator in MongoDB with Mongoose

Conclusion

Understanding how to use the maximum operator, also known as the $max operator; and how it behaves differently based on the values specified.

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 172