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

$type projection operator (cast types within aggregation pipelines).

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
    • Fully Compatible

      Currently there is no way to convert types without the aggregation framework.

      This would be useful to allow things like Dates <-> Timestamps/Strings, Strings <-> Numbers and even cooler stuff like OID <-> String.

      I think the most intuitive way to add type casting would be to reuse the $type query in the $project stage of the aggregation pipeline which would maybe look something like this:

      db.col.aggregate([{
      	$project: {
      		my_date: { $type: ["$my_date", 16] }
      	}
      }])
      

      Where "$my_date" is currently a `Date` bson type and will be casted to a 32-bit integer (16).

      Perhaps aliases could also be supported to allow for:

      db.col.aggregate([{
      	$project: {
      		my_date: { $type: ["$my_date","int"] }
      	}
      }])
      

      Ultimately syntax doesn't matter but I think type casting is something the aggregation framework is lacking which would help many use cases without relying on in app implementations.

            Assignee:
            Unassigned Unassigned
            Reporter:
            dylanpiercey Dylan Piercey
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: