[SERVER-22886] $type projection operator (cast types within aggregation pipelines). Created: 29/Feb/16  Updated: 26/Apr/19  Resolved: 29/Feb/16

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

Type: New Feature Priority: Minor - P4
Reporter: Dylan Piercey Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: Feature
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-6203 Aggregation operators should have wel... Backlog
Backwards Compatibility: Fully Compatible
Participants:

 Description   

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.



 Comments   
Comment by Ian Whalen (Inactive) [ 26/Apr/19 ]

Switching "Drivers Changes Needed" from "Maybe" to "Not Needed" since this was closed as something other than Fixed.

Comment by Charlie Swanson [ 29/Feb/16 ]

Hi dylanpiercey,

Thanks for reporting this issue, but it looks like we already have a similar request in SERVER-6203, so I'm closing this issue as a duplicate. If you do not believe this is a duplicate issue, please feel free to re-open and explain the differences.

Thanks

Generated at Thu Feb 08 04:01:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.