-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
shell-types.txt
239 lines (148 loc) · 5.72 KB
/
shell-types.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
=================================
Data Types in the ``mongo`` Shell
=================================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
MongoDB :term:`BSON` provides support for additional data types than
:term:`JSON`. :ecosystem:`Drivers </drivers>` provide native
support for these data types in host languages and the
:binary:`~bin.mongo` shell also provides several helper classes to support
the use of these data types in the :binary:`~bin.mongo` JavaScript
shell. See the :doc:`Extended JSON </reference/mongodb-extended-json>`
reference for additional information.
.. _mongo-shell-data-type:
Types
-----
.. _mongo-shell-date-type:
Date
~~~~
The :binary:`~bin.mongo` shell provides various methods to return the date,
either as a string or as a ``Date`` object:
- ``Date()`` method which returns the current date as a string.
- ``new Date()`` constructor which returns a ``Date`` object using the
``ISODate()`` wrapper.
- ``ISODate()`` constructor which returns a ``Date`` object using the
``ISODate()`` wrapper.
.. include:: /includes/fact-bson-date-internals.rst
Return Date as a String
```````````````````````
To return the date as a string, use the ``Date()`` method, as in the
following example:
.. code-block:: javascript
var myDateString = Date();
To print the value of the variable, type the variable name in the
shell, as in the following:
.. code-block:: javascript
myDateString
The result is the value of ``myDateString``:
.. code-block:: javascript
Wed Dec 19 2012 01:03:25 GMT-0500 (EST)
To verify the type, use the ``typeof`` operator, as in the following:
.. code-block:: javascript
typeof myDateString
The operation returns ``string``.
Return ``Date``
```````````````
The :binary:`~bin.mongo` shell wraps objects of ``Date`` type with the
``ISODate`` helper; however, the objects remain of type ``Date``.
The following example uses both the ``new Date()`` constructor and the
``ISODate()`` constructor to return ``Date`` objects.
.. code-block:: javascript
var myDate = new Date();
var myDateInitUsingISODateWrapper = ISODate();
You can use the ``new`` operator with the ``ISODate()`` constructor as
well.
To print the value of the variable, type the variable name in the
shell, as in the following:
.. code-block:: javascript
myDate
The result is the ``Date`` value of ``myDate`` wrapped in the
``ISODate()`` helper:
.. code-block:: javascript
ISODate("2012-12-19T06:01:17.171Z")
To verify the type, use the ``instanceof`` operator, as in the
following:
.. code-block:: javascript
myDate instanceof Date
myDateInitUsingISODateWrapper instanceof Date
The operation returns ``true`` for both.
ObjectId
~~~~~~~~
The :binary:`~bin.mongo` shell provides the ``ObjectId()`` wrapper class
around the :ref:`objectid` data type. To generate a new ObjectId, use
the following operation in the :binary:`~bin.mongo` shell:
.. code-block:: javascript
new ObjectId
.. see:: :method:`ObjectId`
.. _shell-type-long:
NumberLong
~~~~~~~~~~
By default, the :binary:`~bin.mongo` shell treats all numbers as
floating-point values. The :binary:`~bin.mongo` shell provides the
``NumberLong()`` wrapper to handle 64-bit integers.
The ``NumberLong()`` wrapper accepts the long as a string:
.. code-block:: javascript
NumberLong("2090845886852")
The following examples use the ``NumberLong()`` wrapper to write to the
collection:
.. code-block:: javascript
db.collection.insert( { _id: 10, calc: NumberLong("2090845886852") } )
db.collection.update( { _id: 10 },
{ $set: { calc: NumberLong("2555555000000") } } )
db.collection.update( { _id: 10 },
{ $inc: { calc: NumberLong(5) } } )
Retrieve the document to verify:
.. code-block:: javascript
db.collection.findOne( { _id: 10 } )
In the returned document, the ``calc`` field contains a
``NumberLong`` object:
.. code-block:: sh
{ "_id" : 10, "calc" : NumberLong("2555555000005") }
If you use the :update:`$inc` to increment the value of a field that
contains a ``NumberLong`` object by a **float**, the data type changes
to a floating point value, as in the following example:
#. Use :update:`$inc` to increment the ``calc`` field by ``5``, which the
:binary:`~bin.mongo` shell treats as a float:
.. code-block:: javascript
db.collection.update( { _id: 10 },
{ $inc: { calc: 5 } } )
#. Retrieve the updated document:
.. code-block:: javascript
db.collection.findOne( { _id: 10 } )
In the updated document, the ``calc`` field contains a floating
point value:
.. code-block:: sh
{ "_id" : 10, "calc" : 2555555000010 }
.. _shell-type-int:
NumberInt
~~~~~~~~~
By default, the :binary:`~bin.mongo` shell treats all numbers as
floating-point values. The :binary:`~bin.mongo` shell provides the
``NumberInt()`` constructor to explicitly specify 32-bit integers.
.. _check-types-in-shell:
Check Types in the ``mongo`` Shell
----------------------------------
To determine the type of fields, the :binary:`~bin.mongo` shell provides
the ``instanceof`` and ``typeof`` operators.
``instanceof``
~~~~~~~~~~~~~~
``instanceof`` returns a boolean to test if a value is an instance of
some type.
For example, the following operation tests whether the ``_id`` field is
an instance of type ``ObjectId``:
.. code-block:: javascript
mydoc._id instanceof ObjectId
The operation returns ``true``.
``typeof``
~~~~~~~~~~
``typeof`` returns the type of a field.
For example, the following operation returns the type of the ``_id``
field:
.. code-block:: javascript
typeof mydoc._id
In this case ``typeof`` will return the more generic ``object`` type
rather than ``ObjectId`` type.