[SERVER-5239] $project should have numeric type coercion operators Created: 07/Mar/12  Updated: 06/Dec/22  Resolved: 16/Feb/18

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

Type: New Feature Priority: Major - P3
Reporter: Daniel Pasette (Inactive) Assignee: Backlog - Query Team (Inactive)
Resolution: Duplicate Votes: 13
Labels: expression
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-6203 Aggregation operators should have wel... Backlog
Duplicate
duplicates SERVER-32784 Add $convert with support for typecas... Closed
Related
related to SERVER-8582 Extend document expression language i... Closed
related to SERVER-6166 consider expanding to wider data type... Closed
is related to SERVER-5209 can't compare values of BSON types 16... Closed
is related to SERVER-16418 Add a way to convert a number or stri... Closed
Assigned Teams:
Query
Participants:

 Description   

We should be able to force conversion between NumberInt(), NumberLong(), and JavaScript double precision floating point numbers in order to normalize the output from situations like that in SERVER-5209. The best thing to do would be to have explicit coercion expressions available in $project.



 Comments   
Comment by David Storch [ 16/Feb/18 ]

Hi bond,

We actually are working on implementing type conversion expressions in the expression language as part of the 3.7.x development release series. In fact, commit 3fe0f3a062397 under SERVER-32784 (pushed to the master branch yesterday) adds support for a $convert expression that supports numerical typecasts. You should now be able to do something like this:

> db.c.drop()
true
> db.c.insert({num: NumberInt(3)})
WriteResult({ "nInserted" : 1 })
> db.c.insert({num: NumberLong(4)})
WriteResult({ "nInserted" : 1 })
> db.c.insert({num: 5.2})
WriteResult({ "nInserted" : 1 })
> db.c.aggregate([{$project: {out: {$convert: {input: "$num", to: "double"}}}}])
{ "_id" : ObjectId("5a871d1d5cf7d58f48c8a43c"), "out" : 3 }
{ "_id" : ObjectId("5a871d225cf7d58f48c8a43d"), "out" : 4 }
{ "_id" : ObjectId("5a871d2a5cf7d58f48c8a43e"), "out" : 5.2 }
> db.c.aggregate([{$project: {out: {$convert: {input: "$num", to: "int"}}}}])
{ "_id" : ObjectId("5a871d1d5cf7d58f48c8a43c"), "out" : 3 }
{ "_id" : ObjectId("5a871d225cf7d58f48c8a43d"), "out" : 4 }
{ "_id" : ObjectId("5a871d2a5cf7d58f48c8a43e"), "out" : 5 }

Since $convert and related features are still under development, we don't have documentation to point you to yet. The documentation should be complete as release candidates for the stable release containing $convert become available.

I'm going to resolve this as a duplicate of SERVER-32784. In the meantime, let me know if you have any questions.

Best,
Dave

Comment by Raviteja [ 15/Feb/18 ]

Any idea when this will be implemented?

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