From 3d768cef161dcc4dc417a043409c58501110fd7d Mon Sep 17 00:00:00 2001 From: ravind Date: Tue, 3 Nov 2015 13:47:38 -0500 Subject: [PATCH] DOCS-5883 : Add 'Number' alias option for $type Round 2: CR changes Round 1: * Added 'Number' Alias to type.txt * Minor formatting edits / cleanups --- source/includes/fact-bson-types.rst | 2 +- source/reference/operator/query/type.txt | 71 +++++++++++++++++++----- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/source/includes/fact-bson-types.rst b/source/includes/fact-bson-types.rst index 573b690c475..880c2517a9e 100644 --- a/source/includes/fact-bson-types.rst +++ b/source/includes/fact-bson-types.rst @@ -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" ======================= ========== ====================== ================================== diff --git a/source/reference/operator/query/type.txt b/source/reference/operator/query/type.txt index 5dd1ec8da20..4cec3606179 100644 --- a/source/reference/operator/query/type.txt +++ b/source/reference/operator/query/type.txt @@ -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 ~~~~~~ @@ -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: @@ -180,7 +225,7 @@ contains ``minKey``: .. code-block:: javascript db.restaurant.find( - { 'grades.grade' : { $type : 'minKey' } } + { "grades.grade" : { $type : "minKey" } } ) This returns @@ -214,7 +259,7 @@ contains ``maxKey``: .. code-block:: javascript db.restaurant.find( - { 'grades.grade' : { $type : 'maxKey' } } + { "grades.grade" : { $type : "maxKey" } } ) This returns @@ -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