[SERVER-13447] provide $projection operator to get type of field Created: 01/Apr/14  Updated: 05/Dec/16  Resolved: 15/Mar/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 3.3.4

Type: Improvement Priority: Major - P3
Reporter: Asya Kamsky Assignee: Benjamin Murphy
Resolution: Done Votes: 0
Labels: expression
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CSHARP-1609 provide $projection operator to get t... Closed
Documented
is documented by DOCS-9591 Docs for SERVER-13447: provide $proje... Closed
Related
is related to DRIVERS-297 Aggregation Framework Support for 3.4 Closed
is related to SERVER-18210 Add query for document structure Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 11 (03/14/16)
Participants:

 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.



 Comments   
Comment by Benjamin Murphy [ 15/Mar/16 ]

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.

Comment by Githook User [ 15/Mar/16 ]

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

Comment by Charlie Swanson [ 11/Mar/16 ]

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

Comment by Asya Kamsky [ 23/Jul/15 ]

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

Comment by Asya Kamsky [ 15/Jul/15 ]

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.

Comment by Asya Kamsky [ 14/Jul/15 ]

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

Comment by Asya Kamsky [ 17/May/14 ]

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

Generated at Thu Feb 08 03:31:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.