$log (aggregation)
定義
$log
Calculates the log of a number in the specified base and returns the result as a double.
$log
の構文は次のとおりです。{ $log: [ <number>, <base> ] } The
<number>
expression can be any valid 式 as long as it resolves to a non-negative number.The
<base>
expression can be any valid 式 as long as it resolves to a positive number greater than1
.式の詳細については、「式演算子」を参照してください。
動作
デフォルトの戻り値の型は double
です。少なくとも1つのオペランドが decimal
の場合、戻り値の型は10進数です。
If either argument resolves to a value of null
or refers to a field that is
missing, $log
returns null
. If either argument resolves to
NaN
, $log
returns NaN
.
例 | 結果 |
---|---|
|
|
|
|
例
コレクション integers
には次のドキュメントが含まれています。
db.integers.insertMany( [ { _id: 1, int: 5 }, { _id: 2, int: 2 }, { _id: 3, int: 23 }, { _id: 4, int: 10 } ] )
The following example uses log 2 in its calculation to
determine the number of bits required to represent the value of
int
.
db.integers.aggregate([ { $project: { bitsNeeded: { $floor: { $add: [ 1, { $log: [ "$int", 2 ] } ] } } } } ])
この操作は次の結果を返します。
{ "_id" : 1, "bitsNeeded" : 3 } { "_id" : 2, "bitsNeeded" : 2 } { "_id" : 3, "bitsNeeded" : 5 } { "_id" : 4, "bitsNeeded" : 4 }
以下も参照してください。