$ln
The `$ln` operator calculates the natural logarithm (base e) of the input number.
Syntax
{ $ln: <number> }
Parameters
<number>
stringrequiredAny valid expression that resolves to a positive number.
Examples
Sample Data
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"sales": {
"salesByCategory": [
{ "categoryName": "Wine Accessories", "totalSales": 34440 }
]
}
}
Calculate the natural logarithm of total sales by category
Calculate the natural logarithm of sales volume by category to analyze growth rates.
Query:
db.stores.aggregate([
{ $match: { _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" } },
{
$project: {
name: 1,
salesGrowthMetrics: {
$map: {
input: "$sales.salesByCategory",
as: "category",
in: {
categoryName: "$$category.categoryName",
salesValue: "$$category.totalSales",
naturalLog: { $ln: "$$category.totalSales" }
}
}
}
}
}
])
Output:
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub",
"salesGrowthMetrics": [
{ "categoryName": "Sound Bars", "salesValue": 2120, "naturalLog": 7.659 }
]
}
]