$ln(算术表达式)

适用对象: MongoDB vCore

$ln 运算符计算数字的自然对数(base e),并返回结果。

语法

运算符的 $isArray 语法如下所示:

{ $ln: <number> }

参数

DESCRIPTION
<number> 解析为正数的任何有效表达式。

示例:

让我们了解数据集中的示例数据的 stores 使用情况,以分析销售增长率。

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" }
          }
        }
      }
    }
  }
])

这将生成以下输出:

{
  "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
  "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
  "salesGrowthMetrics": [
    {
      "categoryName": "Sound Bars",
      "salesValue": 2120,
      "naturalLog": 7.659
    },
    {
      "categoryName": "Home Theater Projectors",
      "salesValue": 45004,
      "naturalLog": 10.714
    },
    {
      "categoryName": "Game Controllers",
      "salesValue": 43522,
      "naturalLog": 10.681
    },
    {
      "categoryName": "Remote Controls",
      "salesValue": 28946,
      "naturalLog": 10.273
    },
    {
      "categoryName": "VR Games",
      "salesValue": 32272,
      "naturalLog": 10.382
    }
  ]
}