當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


MongoDB $exp用法及代碼示例

MongoDB 提供了不同類型的中使用的算術表達式運算符聚合管道階段$exp操作符就是其中之一。這操作員用於提高歐拉數(即e) 到指定的 index 並返回結果。

用法:

{ $exp:<exponent> }


在這裏, index 是一個有效的表達式,直到它解析為一個數字。

  • 如果輸入的值解析為 null,則此運算符將返回 null。
  • 如果輸入的值解析為 NaN,則此運算符將返回 NaN。
  • 如果輸入的值引用了缺失的字段,則此運算符將返回 null。

例子:

在以下示例中,我們正在使用:



Database: GeeksforGeeks

Collection:example

Document: two documents that contain the details of the shapes in the form of field-value pairs.

使用 $exp 運算符:

在此示例中,我們將找到側字段值的 index 。

db.example.aggregate([ {$match:{name:"Square"}},
... {$project:{result:{$exp:"$side"}}}])

在嵌入式文檔中使用 $exp 運算符:

在這個例子中,我們將找到measurement.height 和measurement.width 字段值的差的 index 。

db.example.aggregate([ {$match:{name:"Rectangle"}},
... {$project:{result:{$exp:
... {$subtract:["$measurement.height", "$measurement.width"]}}}}])

相關用法


注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 MongoDB $exp Operator。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。