Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-13447

provide $projection operator to get type of field

    Details

      Description

      Syntax

      {$type: "$fieldName"}
      

      Examples

      Input

      {_id: NumberLong(4)}
      

      Pipeline

      db.coll.aggregate([{
          $project: {
              _id: {$type: "$_id"}
          }
      }])
      

      Output

      {_id: "long"}
      

      Additional Notes

      • Type names will match the query $type operator.
      • Note in a query, the $type will try to match elements inside of an array, so a {$type: 'array'} will only match nested arrays. This will not be the case in aggregation. The type of an array will be 'array'.
      • Adding a $in expression (SERVER-6146) and a $switch expression (SERVER-10689) should make it easier to tell if something is numeric.

      Original Description

      If there was an equivalent of $type field (it can return bson type number or something else) it would be highly useful to do appropriate processing only if a field is a particular type.

      For example, without knowing if a field is always an array, it's possible to get an error trying to $unwind or to get $size - if you could

      {$project: {typeOfFoo:{$type:"$foo"}}}
      

      you could now do different processing/operations depending on whether type is an array or not, or if it's a string vs number vs date, etc.

        Issue Links

          Activity

          Hide
          asya Asya Kamsky added a comment -

          meanwhile I did find a workaround for checking if something is an array:

          Projecting a field that has value "$array.0" will give you something you can check - if it's null field was not an array, if it's [ ] then it was an array.

          Described/demoed here:
          http://www.kamsky.org/stupid-tricks-with-mongodb/is-it-an-array

          Show
          asya Asya Kamsky added a comment - meanwhile I did find a workaround for checking if something is an array: Projecting a field that has value "$array.0" will give you something you can check - if it's null field was not an array, if it's [ ] then it was an array. Described/demoed here: http://www.kamsky.org/stupid-tricks-with-mongodb/is-it-an-array
          Hide
          asya Asya Kamsky added a comment -

          As of 3.1.5 there is an $isArray operator in $project which returns true if field is an array (SERVER-18222).

          Show
          asya Asya Kamsky added a comment - As of 3.1.5 there is an $isArray operator in $project which returns true if field is an array ( SERVER-18222 ).
          Hide
          asya Asya Kamsky added a comment -

          Turns out since 3.0 added total ordering across all types it is possible to generate types in project stage with an if-then-else type structure/expression.

          Show
          asya Asya Kamsky added a comment - Turns out since 3.0 added total ordering across all types it is possible to generate types in project stage with an if-then-else type structure/expression.
          Hide
          asya Asya Kamsky added a comment -

          If anyone is looking for a workaround based on total type ordering, I show an example of it here:
          http://www.kamsky.org/stupid-tricks-with-mongodb/determining-type-of-field-in-mongodb-aggregation

          Show
          asya Asya Kamsky added a comment - If anyone is looking for a workaround based on total type ordering, I show an example of it here: http://www.kamsky.org/stupid-tricks-with-mongodb/determining-type-of-field-in-mongodb-aggregation
          Hide
          charlie.swanson Charlie Swanson added a comment -

          After some internal discussion, I've updated the description to match the agreed-upon design.

          Show
          charlie.swanson Charlie Swanson added a comment - After some internal discussion, I've updated the description to match the agreed-upon design.
          Hide
          xgen-internal-githook Githook User added a comment -

          Author:

          {u'username': u'benjaminmurphy', u'name': u'Benjamin Murphy', u'email': u'benjamin_murphy@me.com'}

          Message: SERVER-13447 Added type expression to aggregation.
          Branch: master
          https://github.com/mongodb/mongo/commit/a9d3450f9a40c6ef02b5713c9f0841aa424a327a

          Show
          xgen-internal-githook Githook User added a comment - Author: {u'username': u'benjaminmurphy', u'name': u'Benjamin Murphy', u'email': u'benjamin_murphy@me.com'} Message: SERVER-13447 Added type expression to aggregation. Branch: master https://github.com/mongodb/mongo/commit/a9d3450f9a40c6ef02b5713c9f0841aa424a327a
          Hide
          benjamin.murphy Benjamin Murphy (Inactive) added a comment -

          This patch adds a new aggregation expression, $type, which produces a string representing the type of the input field. It will need to be documented here: https://docs.mongodb.org/manual/meta/aggregation-quick-reference/#aggregation-expressions

          Drivers will need to be updated to properly translate the new expression as well.

          Show
          benjamin.murphy Benjamin Murphy (Inactive) added a comment - This patch adds a new aggregation expression, $type, which produces a string representing the type of the input field. It will need to be documented here: https://docs.mongodb.org/manual/meta/aggregation-quick-reference/#aggregation-expressions Drivers will need to be updated to properly translate the new expression as well.

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                  Agile