Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
$min
The $min operator returns the minimum value from an array of values. It can be used in aggregation stages to find the minimum value for a specified field across multiple documents.
Parameters
Example (MongoDB Shell)
The following example demonstrates the usage of the $min operator to find the minimum value of the age field across multiple documents.
Create sample documents
db.users.insertMany([
{ name: "John", age: 35 },
{ name: "Jane", age: 28 },
{ name: "Bob", age: 42 },
{ name: "Alice", age: 31 }
]);
Query example
db.users.aggregate([
{ $group: { _id: null, minAge: { $min: "$age" } } },
{ $project: { _id: 0, minAge: 1 } }
])
Output
[ { minAge: 28 } ]
Code examples
To view a code example for using the $min command, choose the tab for the language that you want to use:
- Node.js
-
const { MongoClient } = require('mongodb');
async function findMinAge() {
const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
const db = client.db('test');
const users = db.collection('users');
const result = await users.aggregate([
{ $group: {
_id: null,
minAge: { $min: "$age" }
}}
]).toArray();
console.log(result);
client.close();
}
findMinAge();
- Python
-
from pymongo import MongoClient
def find_min_age():
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
db = client['test']
users = db.users
result = list(users.aggregate([
{ "$group": {
"_id": None,
"minAge": { "$min": "$age" }
}}
]))
print(result)
client.close()
find_min_age()