Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-9591

Docs for SERVER-13447: provide $projection operator to get type of field

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • # Replies:
      1
    • Last comment by Customer:
      true

      Description

      Engineering Ticket 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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              emily.hall Emily Hall
              Participants:
              Last commenter:
              Kay Kim (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since reply:
                2 years, 34 weeks, 2 days ago
                Date of 1st Reply: