Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB Aggregation

MongoDB $min Operator

Updated on Oct 31, 2024
By Mari Selvan
👁️ 17 - Views
⏳ 4 mins
💬 1 Comment
MongoDB $min Operator

Photo Credit to CodeToFun

🙋 Introduction

In MongoDB's aggregation framework, the $min operator emerges as a pivotal tool for identifying the minimum value within a dataset. This operator proves to be instrumental in various scenarios where you need to extract the smallest value from a set of documents.

Let's delve into the details of how the $min operator functions within MongoDB's aggregation pipelines.

💡 Syntax

The syntax for the $min method is straightforward:

syntax.js
Copied
Copy To Clipboard
{ $min: <expression> }
  • $min: This operator signifies that the subsequent operation will compute the minimum value.
  • <expression>: This represents the expression that evaluates to the values from which the minimum will be computed. It could be a field reference, a mathematical expression, or a value.

📝 Example

⌨️ Input

Consider a collection named products containing documents with fields name and price, representing product information. Here are sample documents from the products collection:

Sample Input
Copied
Copy To Clipboard
[
  {
    "_id": ObjectId("609c26812e9274a86871bc6a"),
    "name": "Product A",
    "price": 25
  },
  {
    "_id": ObjectId("609c26812e9274a86871bc6b"),
    "name": "Product B",
    "price": 15
  },
  {
    "_id": ObjectId("609c26812e9274a86871bc6c"),
    "name": "Product C",
    "price": 20
  },
  {
    "_id": ObjectId("609c26812e9274a86871bc6d"),
    "name": "Product D",
    "price": 10
  }
]

🔄 Aggregation

Now, suppose we want to find the minimum price among all products. Here's how you can achieve this using the $min operator:

example.js
Copied
Copy To Clipboard
db.products.aggregate([
  {
    $group: {
      _id: null,
      minPrice: { $min: "$price" }
    }
  }
])

🧩 Explanation

  • $group: This stage groups all documents into a single group (since _id: null) and computes the minimum price within that group.
  • $min: Calculates the minimum value of the specified field, which in this case is the price.

💻 Output

Now, let's take a look at the output generated by the aggregation pipeline:

Output
Copied
Copy To Clipboard
{ "_id": null, "minPrice": 10 }

📚 Use Cases

  1. Price Analysis:

    The $min operator is invaluable for extracting the lowest price from a collection of products, facilitating price analysis and comparison.

  2. Statistical Aggregation:

    When performing statistical analyses, identifying the minimum value is often a crucial step in understanding the distribution of data.

  3. Quality Control:

    In manufacturing or production environments, the $min operator can be used to monitor and ensure that certain parameters remain above a minimum threshold.

🎉 Conclusion

The $min operator in MongoDB's aggregation framework serves as a powerful tool for extracting the smallest value within datasets, enabling users to glean valuable insights and make informed decisions based on minimum values. Whether you're analyzing prices, performing statistical computations, or monitoring quality control parameters, mastering the usage of $min empowers you to efficiently extract minimum values within MongoDB aggregation pipelines.

With its intuitive syntax and versatile applications, the $min operator proves to be an indispensable asset for handling numerical data effectively within MongoDB. Incorporate it into your aggregation pipelines to unlock new dimensions of data analysis and gain deeper insights into your datasets.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy