/
toInt.txt
201 lines (136 loc) · 4.93 KB
/
toInt.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
======================
$toInt (aggregation)
======================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
.. meta::
:description: type conversion, convert to integer, integer conversion, aggregation, convert to int
:keywords: type conversion, convert to integer, integer conversion, aggregation, convert to int
Definition
----------
.. expression:: $toInt
.. versionadded:: 4.0
Converts a value to an integer. If the value cannot be converted
to an integer, :expression:`$toInt` errors. If the value is null or
missing, :expression:`$toInt` returns null.
:expression:`$toInt` has the following syntax:
.. code-block:: javascript
{
$toInt: <expression>
}
The :expression:`$toInt` takes any valid :ref:`expression
<aggregation-expressions>`.
The :expression:`$toInt` is a shorthand for the following
:expression:`$convert` expression:
.. code-block:: javascript
{ $convert: { input: <expression>, to: "int" } }
.. seealso:: :expression:`$convert`
Behavior
--------
The following table lists the input types that can be converted to an
integer:
.. list-table::
:header-rows: 1
:widths: 20 80
* - Input Type
- Behavior
* - Boolean
- | Returns ``0`` for ``false``.
| Returns ``1`` for ``true``.
* - Double
- Returns truncated value.
The truncated double value must fall within the minimum and
maximum value for an integer.
You cannot convert a double value whose truncated value is less
than the minimum integer value or is greater than the maximum
integer value.
* - Decimal
- Returns truncated value.
The truncated decimal value must fall within the minimum and
maximum value for an integer.
You cannot convert a decimal value whose truncated value is less
than the minimum integer value or is greater than the maximum
integer value.
* - Integer
- No-op. Returns the integer value.
* - Long
- Returns the long value as an integer.
The long value must fall within the minimum and maximum value
for an integer.
You cannot convert a long value that is less than the minimum
integer value or is greater than the maximum integer value.
* - String
- Returns the numerical value of the string as an integer.
The string value must be a base\ :sub:`10` integer; e.g.
``"-5"``, ``"123456"``).
You cannot convert a string value of a float or decimal or
non-base\ :sub:`10` number (e.g. ``"-5.0"``, ``"0x6400"``)
The following table lists some conversion to integer examples:
.. list-table::
:header-rows: 1
:widths: 80 20
* - Example
- Results
* - ``$toInt: true``
- 1
* - ``$toInt: false``
- 0
* - ``$toInt: 1.99999``
- 1
* - ``$toInt: NumberDecimal("5.5000")``
- 5
* - ``$toInt: NumberDecimal("9223372036000.000")``
- Error
* - ``$toInt: NumberLong("5000")``
- 5000
* - ``$toInt: NumberLong("922337203600")``
- Error
* - ``$toInt: "-2"``
- -2
* - ``$toInt: "2.5"``
- Error
* - ``$toInt: null``
- null
Example
-------
Create a collection ``orders`` with the following documents:
.. code-block:: javascript
db.orders.insert( [
{ _id: 1, item: "apple", qty: 5, price: 10 },
{ _id: 2, item: "pie", qty: 10, price: NumberDecimal("20.0") },
{ _id: 3, item: "ice cream", qty: 2, price: "4.99" },
{ _id: 4, item: "almonds" , qty: 5, price: 5 }
] )
The following aggregation operation on the ``orders`` collection
converts the ``qty`` to an integer as well as convert ``price`` to a
decimal before calculating the total price:
.. code-block:: javascript
// Define stage to add convertedPrice and convertedQty fields with the converted price and qty values
priceQtyConversionStage = {
$addFields: {
convertedPrice: { $toDecimal: "$price" },
convertedQty: { $toInt: "$qty" },
}
};
// Define stage to calculate total price by multiplying convertedPrice and convertedQty fields
totalPriceCalculationStage = {
$project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }
};
db.orders.aggregate( [
priceQtyConversionStage,
totalPriceCalculationStage
])
The operation returns the following documents:
.. code-block:: javascript
{ "_id" : 1, "item" : "apple", "totalPrice" : NumberDecimal("50.0000000000000") }
{ "_id" : 2, "item" : "pie", "totalPrice" : NumberDecimal("200.0") }
{ "_id" : 3, "item" : "ice cream", "totalPrice" : NumberDecimal("9.98") }
{ "_id" : 4, "item" : "almonds", "totalPrice" : NumberDecimal("25.00000000000000") }
.. note::
If the conversion operation encounters an error, the aggregation
operation stops and throws an error. To override this behavior, use
:expression:`$convert` instead.