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


MongoDB $divide用法及代碼示例

MongoDB 提供了不同類型的中使用的算術表達式運算符聚合管道階段$divide操作符就是其中之一。這操作員習慣於劃分一個數乘另一個數並返回結果分配。

用法:

{ $divide: [ <expression1>, <expression2> ] }

在這裏,在此運算符中,參數以數組形式傳遞。第一個參數是紅利,第二個參數是除數。噸他的參數必須是一個有效的表達式,直到它解析為一個數字。

例子:

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



Database: GeeksforGeeks

Collection:employee

Document: three documents that contain the details of the employees in the form of field-value pairs.

使用 $divide 運算符進行除法:

在這個例子中,我們將開發部門員工的工資分成兩半。

db.employee.aggregate([{$match:{department:"Development"}},
... {$project:{name:1, halfSalary:{$divide:["$salary", 2]}}}])


在嵌入的文檔中使用 $divide 運算符進行除法:

在這個例子中,我們將人力資源部門員工的工資分成兩半。

db.employee.aggregate([{$match:{department:"HR"}},
... {$project:{name:1, halfsalary:
               {$divide:["$details.salary", 2]}}}])


相關用法


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