/
divide.txt
65 lines (44 loc) · 1.62 KB
/
divide.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
=====================
$divide (aggregation)
=====================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. expression:: $divide
Divides one number by another and returns the result. Pass the
arguments to :expression:`$divide` in an array.
The :expression:`$divide` expression has the following syntax:
.. code-block:: javascript
{ $divide: [ <expression1>, <expression2> ] }
The first argument is the dividend, and the second argument is the
divisor; i.e. the first argument is divided by the second argument.
The arguments can be any valid :ref:`expression
<aggregation-expressions>` as long as they resolve to numbers. For
more information on expressions, see :ref:`aggregation-expressions`.
Examples
--------
Consider a ``conferencePlanning`` collection with the following documents:
.. code-block:: javascript
db.conferencePlanning.insertMany( [
{ "_id" : 1, "city" : "New York", "hours" : 80, "tasks" : 7 },
{ "_id" : 2, "city" : "Singapore", "hours" : 40, "tasks" : 4 }
] )
The following aggregation uses the :expression:`$divide` expression to
divide the ``hours`` field by a literal ``8`` to compute the number of
work days:
.. code-block:: javascript
db.planning.aggregate(
[
{ $project: { city: 1, workdays: { $divide: [ "$hours", 8 ] } } }
]
)
The operation returns the following results:
.. code-block:: javascript
:copyable: false
{ "_id" : 1, "city" : "New York", "workdays" : 10 }
{ "_id" : 2, "city" : "Singapore", "workdays" : 5 }