정의
행동
Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $mul with an empty operand expression ( { } ). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).
MongoDB 5.0부터 업데이트 연산자는 문자열 기반 이름이 있는 문서 필드를 사전순으로 처리합니다. 숫자 이름이 있는 필드는 숫자 순서대로 처리됩니다. 자세한 내용은 업데이트 운영자 동작을 참조하십시오.
누락된 필드
If the field does not exist in a document, $mul creates the field and sets the value to zero of the same numeric type as the multiplier.
원자
$mul 는 단일 문서 내에서 원자적으로 수행되는 연산입니다.
혼합 유형
혼합된 숫자 유형(32비트 정수, 64비트 정수, Double, Decimal128)의 값과의 곱셈은 숫자 유형의 변환을 초래할 수 있습니다. 혼합 숫자 형식의 값을 곱하는 경우 다음 형식 변환 규칙이 적용됩니다.
데이터 유형 | 32비트 정수 | 64비트 정수 | Double | Decimal128 |
|---|---|---|---|---|
32비트 정수 | 32비트 또는 64비트 정수 | 64비트 정수 | Double | Decimal128 |
64비트 정수 | 64비트 정수 | 64비트 정수 | Double | Decimal128 |
Double | Double | Double | Double | Decimal128 |
Decimal128 | Decimal128 | Decimal128 | Decimal128 | Decimal128 |
참고
두 개의 32비트 정수의 곱이 32비트 정수의 최대값을 초과하는 경우 결과는 64비트 정수입니다.
64비트 정수의 최대값을 초과하는 모든 유형의 정수 연산에서는 오류가 발생합니다.
예시
필드 값 곱하기
products 컬렉션을 생성합니다.
db.products.insertOne( { "_id" : 1, "item" : "Hats", "price" : Decimal128("10.99"), "quantity" : 25 } )
In the following operation, db.collection.updateOne() updates the document. The $mul operator multiplies the price field by 1.25 and the quantity field by 2:
db.products.updateOne( { _id: 1 }, { $mul: { price: Decimal128( "1.25" ), quantity: 2 } } )
업데이트된 문서에서:
price는 원래 값인 10.99에 1.25를 곱한 값입니다.quantity는 원래 값인 25에 2를 곱한 값입니다.
{ _id: 1, item: 'Hats', price: Decimal128("13.7375"), quantity: 50 }
존재하지 않는 필드에 $mul 연산자 적용
다음 문서를 products 컬렉션에 추가합니다:
db.products.insertOne( { _id: 2, item: "Unknown" } )
In the following operation, db.collection.updateOne() attempts to apply the $mul operator to a field that is not in the document:
db.products.updateOne( { _id: 2 }, { $mul: { price: Decimal128("100") } } )
price필드를 삽입합니다.Decimal128("0")을 설정합니다.
{ "_id" : 2, "item" : "Unknown", "price" : Long(0) }
필드는 price 는 승수와 동일한 유형인 Decimal128입니다.
혼합 숫자 유형 곱하기
다음 문서를 products 컬렉션에 추가합니다:
db.products.insertOne( { _id: 3, item: "Scarf", price: Decimal128("10") } )
In the following operation, db.collection.updateOne() uses the $mul operator to multiply the value in the price field Decimal128(10) by Int32(5):
db.products.updateOne( { _id: 3 }, { $mul: { price: Int32(5) } } )
이 연산을 수행하면 다음 문서가 생성됩니다.
{ _id: 3, item: 'Scarf', price: Decimal128("50") }
The value in the price field is of type Decimal128. See Multiplication Type Conversion Rules for details.