[CSHARP-1609] provide $projection operator to get type of field Created: 18/Mar/16  Updated: 27/May/22  Resolved: 26/Aug/16

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Craig Wilson
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-13447 provide $projection operator to get t... Closed
is depended on by DRIVERS-297 Aggregation Framework Support for 3.4 Closed
Epic Link: MongoDB 3.4
Server Compat: 3.3

 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 Craig Wilson [ 26/Aug/16 ]

There doesn't seem to be a good reason to support this. It would require us creating a new custom extension method and hanging it of IEnumerable and IQueryable. Ultimately, anyone using LINQ already knows the types and doesn't need this functionality.

Generated at Wed Feb 07 21:40:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.