$min Operator in MongoDB: Basic Guide

In this tutorial, I am discussing the $min operator in MongoDB.

Also see: $max Operator in MongoDB

As MongoDB has a variety of operators, you can pick from these to satisfy a variety of purposes. The MongoDB database allows you to develop applications that are capable of performing much more than just CRUD, with its very powerful, easy-to-use, and flexible operators. MongoDB certainly contributes to its popularity in this way.

By adding a broader set of operators, MongoDB is able to handle queries, printing, and updating. No matter what your experience level is, it has something for everyone with its easy-to-use interface.

One of MongoDB’s most useful and commonly used operators, I’ll be discussing the $min operator today.

The $min operator is also known as the minimum operator. Well, it is just the opposite of the maximum operator. This operator can be used to update a given field with the given value if the given value is less than the current value of the field.

In case the given field doesn’t exist, the $min operator will create a new field and set its value. You can use the $min operator with the update functions like update(), updateMany(), and updateOne().

In this guide, I will demonstrate examples so you can get a better idea about how you can use the $min operator in your MongoDB-backed applications.

The $min Operator Syntax

Let us take a quick look at the $min operator syntax:

{ $min: { field1: value1, field2: value2 ... } }

Setting $min Operator Value Less than Field Value

Let us get started with using our $min operator in the mongo shell.

  • Start up the MongoDB service
  • Pick the database you wish to use and move into it:
show dbs 
use droneShop
  • Checking all our documents in the drones collection:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Thunderbird Phoenix ",
        "price" : 6300,
        "weight" : "1.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "FR - Hummingbird",
        "price" : 45000,
        "weight" : "20 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "Dixy Magenta",
        "price" : 4000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "UJ - Spear Eye Forest Green",
        "price" : 10000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 75000,
        "weight" : "5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "P12 - Mercenary SF",
        "price" : 12000,
        "weight" : "2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Meteor Rada W54",
        "price" : 6000,
        "weight" : "7 kilograms",
        "__v" : 0
}
  • Now, let’s say we want to update all the prices of our drones to just 2000. This is where we will use the $min operator.
> db.drones.updateMany( {}, { $min: { price: 2000 } } )

{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
  • Checking all our documents:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Thunderbird Phoenix ",
        "price" : 2000,
        "weight" : "1.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "FR - Hummingbird",
        "price" : 2000,
        "weight" : "20 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "Dixy Magenta",
        "price" : 2000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation"
        ],
        "onSale" : false,
        "name" : "UJ - Spear Eye Forest Green",
        "price" : 2000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 2000,
        "weight" : "5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "P12 - Mercenary SF",
        "price" : 2000,
        "weight" : "2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Meteor Rada W54",
        "price" : 2000,
        "weight" : "7 kilograms",
        "__v" : 0
}

Setting $min Operator Value More than Field Value

Let us now use the $min operator and set the given value more than the current value of the price field.

> db.drones.updateMany( {}, { $min: { price: 20000 } } )

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

As you might have noticed, the “modifiedCount” key equals 0 this time; which is a sign that the $min operator couldn’t change any values in the database. However, let us check our documents once again:

> db.drones.find({}).pretty()

{
        "_id" : ObjectId("61573946341d653bccaf7827"),
        "utility" : [
                "Monitoring or Inspection",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Thunderbird Phoenix ",
        "price" : 2000,
        "weight" : "1.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615739bb341d653bccaf7828"),
        "utility" : [
                "Delivery",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "FR - Hummingbird",
        "price" : 2000,
        "weight" : "20 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f092d0a441e040c3fdc"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Dixy Magenta",
        "price" : 2000,
        "weight" : "4.7 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61589f6e2d0a441e040c3fdd"),
        "utility" : [
                "Photography",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "UJ - Spear Eye Forest Green",
        "price" : 2000,
        "weight" : "3 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61598fe5ebf45d32b85bd1a3"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Golden Pegasus 57B",
        "price" : 2000,
        "weight" : "5 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("61599147ebf45d32b85bd1a4"),
        "utility" : [
                "Security",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "P12 - Mercenary SF",
        "price" : 2000,
        "weight" : "2 kilograms",
        "__v" : 0
}
{
        "_id" : ObjectId("615991e9ebf45d32b85bd1a6"),
        "utility" : [
                "Monitoring or Inspection",
                "Monitoring or Inspection",
                "Security",
                "Recreation",
                "Photography"
        ],
        "onSale" : false,
        "name" : "Meteor Rada W54",
        "price" : 2000,
        "weight" : "7 kilograms",
        "__v" : 0
}

This way, we have learned how to use the $min operator in MongoDB to change the values of the fields to a minimum value from the current value.

Conclusion

Learning to use the MongoDB $min operator with the help of examples.

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 171