MongoDB Aggregation
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:
{ $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:
[
{
"_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:
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:
{ "_id": null, "minPrice": 10 }
📚 Use Cases
Price Analysis:
The
$min
operator is invaluable for extracting the lowest price from a collection of products, facilitating price analysis and comparison.Statistical Aggregation:
When performing statistical analyses, identifying the minimum value is often a crucial step in understanding the distribution of data.
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:
Author
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
If you have any doubts regarding this article (MongoDB $min Operator), please comment here. I will help you immediately.