기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$min
$min 업데이트 연산자는 지정된 값이 현재 필드 값보다 작은 경우에만 필드 값을 업데이트합니다. 이 연산자는 업데이트 전반에 걸쳐 최소값을 유지하는 데 유용합니다.
파라미터
예제(MongoDB 쉘)
다음 예제에서는 $min 연산자를 사용하여 기상 관측소의 기록된 최저 온도를 업데이트하는 방법을 보여줍니다.
샘플 문서 생성
db.weather.insertMany([
{ _id: 1, station: "Station A", lowestTemp: 15 },
{ _id: 2, station: "Station B", lowestTemp: 20 },
{ _id: 3, station: "Station C", lowestTemp: 18 }
])
업데이트 예제
db.weather.updateOne(
{ _id: 1 },
{ $min: { lowestTemp: 12 } }
)
결과
12가 현재 값인 15보다 작기 때문에 스테이션 A의 lowestTemp 필드가 12로 업데이트됩니다.
{ "_id": 1, "station": "Station A", "lowestTemp": 12 }
코드 예제
$min 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.
- Node.js
-
const { MongoClient } = require('mongodb');
async function example() {
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 collection = db.collection('weather');
const result = await collection.updateOne(
{ _id: 1 },
{ $min: { lowestTemp: 12 } }
);
console.log(result);
await client.close();
}
example();
- Python
-
from pymongo import MongoClient
def example():
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
db = client['test']
collection = db['weather']
result = collection.update_one(
{ '_id': 1 },
{ '$min': { 'lowestTemp': 12 } }
)
print(result)
client.close()
example()