Skip to content

DOCS-5883 : Add 'Number' alias option for $type #2434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/includes/fact-bson-types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ JavaScript (with scope) 15 "javascriptWithScope"
32-bit integer 16 "int"
Timestamp 17 "timestamp"
64-bit integer 18 "long"
Min key -1 "minKey"
Min key -1 "minKey"
Max key 127 "maxKey"
======================= ========== ====================== ==================================
71 changes: 58 additions & 13 deletions source/reference/operator/query/type.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ Available Types

.. include:: /includes/fact-bson-types.rst

:query:`$type` supports the ``number`` alias, which will match against the
following :term:`BSON` types:

- double

- 32-bit integer

- 64-bit integer

See :ref:`document-querying-by-data-type`

Arrays
~~~~~~

Expand Down Expand Up @@ -95,30 +106,64 @@ Examples
Querying by Data Type
~~~~~~~~~~~~~~~~~~~~~

Given the collection ``addressBook`` containing addresses and zipcodes, where
``zipCode`` has both ``String`` and ``NumberInt`` values:
The ``addressBook`` contains addresses and zipcodes, where
``zipCode`` has ``string``, ``int``, ``double``, and ``long``
values:

.. code-block:: javascript

{ "_id" : 1, address : "2030 Martian Way", zipCode : "90698345" }
{ "_id" : 2, address : "156 Lunar Place", zipCode : "43339374" }
{ "_id" : 4, address : "55 Saturn Ring" , zipCode : 88602117 }
db.addressBook.insertMany(
[
{ "_id" : 1, address : "2030 Martian Way", zipCode : "90698345" },
{ "_id" : 2, address: "156 Lunar Place", zipCode : 43339374 },
{ "_id" : 3, address : "2324 Pluto Place", zipCode: NumberLong(3921412) },
{ "_id" : 4, address : "55 Saturn Ring" , zipCode : NumberInt(88602117) }
]
)

The following queries return all documents where ``zipCode`` is the
:term:`BSON` type ``string``:


.. code-block:: javascript

db.addressBook.find( { "zipCode" : { $type : 2 } } );
db.addressBook.find( { "zipCode" : { $type : "string" } } );

These queries return:

.. code-block:: javascript

{ "_id" : 1, "address" : "2030 Martian Way", "zipCode" : "90698345" }

The following queries return all documents where ``zipCode`` is the
:term:`BSON` type ``String:``
:term:`BSON` type ``double``:

.. code-block:: javascript

db.addressBook.find( { "zipCode" : { $type : 1 } } )
db.addressBook.find( { "zipCode" : { $type : "double" } } )

These queries return:

.. code-block:: javascript

db.addressBook.find( { zipCode: { $type : 2 } } );
db.addressBook.find( { zipCode: { $type : 'string' } } );
{ "_id" : 2, "address" : "156 Lunar Place", "zip" : 43339374 }

The following query uses the ``number`` alias to return documents where
``zipCode`` is the :term:`BSON` type ``double``, ``int``, or ``long``:

.. code-block:: javascript

db.addressBook.find( { "zipCode" : { $type : "number" } } )

These queries return:

.. code-block:: javascript

{ "_id": 1, address: "2030 Martian Way", zipCode: "90698345" }
{ "_id": 2, address: "156 Lunar Place", zipCode: "43339374" }
{ "_id" : 2, address : "156 Lunar Place", zipCode : 43339374 }
{ "_id" : 3, address : "2324 Pluto Place", zipCode: NumberLong(3921412) }
{ "_id" : 4, address : "55 Saturn Ring" , zipCode : 88602117 }

.. _document-querying-by-MinKey-And-MaxKey:

Expand Down Expand Up @@ -180,7 +225,7 @@ contains ``minKey``:
.. code-block:: javascript

db.restaurant.find(
{ 'grades.grade' : { $type : 'minKey' } }
{ "grades.grade" : { $type : "minKey" } }
)

This returns
Expand Down Expand Up @@ -214,7 +259,7 @@ contains ``maxKey``:
.. code-block:: javascript

db.restaurant.find(
{ 'grades.grade' : { $type : 'maxKey' } }
{ "grades.grade" : { $type : "maxKey" } }
)

This returns
Expand Down Expand Up @@ -246,7 +291,7 @@ This returns
Querying by Array Type
----------------------

The SensorReading collection contains the following documents:
The ``SensorReading`` collection contains the following documents:

.. code-block:: javascript

Expand Down