[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: |
|
||||||||||||||||||||||||||||
| Backwards Compatibility: | Fully Compatible | ||||||||||||||||||||||||||||
| Sprint: | Query 11 (03/14/16) | ||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||
| Description |
Syntax
ExamplesInput
Pipeline
Output
Additional Notes
Original DescriptionIf 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
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: |
| 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: |
| 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 ( |
| 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: |