AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$multiply(式 演算子)

$multiply

Multiplies numbers together and returns the result. Pass the arguments to $multiply in an array.

The $multiply expression has the following syntax:

{ $multiply: [ <expression1>, <expression2>, ... ] }

引数は、数値に変換される限り、どのような有効なでもかまいません。 式の詳細については、「式 」を参照してください。

MongoDB 6.1 以降では、 $multiply 操作を最適化できます。パフォーマンスを向上させるには、参照を引数リストの最後にグループ化します。たとえば、

$multiply: [ 1, 2, 3, '$a', '$b', '$c' ]

以下のドキュメントを持つsalesコレクションを考えてみましょう。

db.sales.insertMany( [
{ _id : 1, "item" : "abc", "price" : 10, "quantity": 2, date: ISODate("2014-03-01T08:00:00Z") },
{ _id : 2, "item" : "jkl", "price" : 20, "quantity": 1, date: ISODate("2014-03-01T09:00:00Z") },
{ _id : 3, "item" : "xyz", "price" : 5, "quantity": 10, date: ISODate("2014-03-15T09:00:00Z") }
] )

The following aggregation uses the $multiply expression in the $project pipeline to multiply the price and the quantity fields:

db.sales.aggregate(
[
{ $project: { date: 1, item: 1, total: { $multiply: [ "$price", "$quantity" ] } } }
]
)

この操作は次の結果を返します。

{ "_id" : 1, "item" : "abc", "date" : ISODate("2014-03-01T08:00:00Z"), "total" : 20 }
{ "_id" : 2, "item" : "jkl", "date" : ISODate("2014-03-01T09:00:00Z"), "total" : 20 }
{ "_id" : 3, "item" : "xyz", "date" : ISODate("2014-03-15T09:00:00Z"), "total" : 50 }
このページを評価

項目一覧