[SERVER-11400] Need a type conversion mechanism to convert between strings and numbers Created: 28/Oct/13 Updated: 06/Dec/22 Resolved: 06/Mar/18 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | 2.5.3 |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Asya Kamsky | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Duplicate | Votes: | 15 |
| Labels: | expression, usability | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||
| Assigned Teams: |
Query
|
||||||||||||||||||||||||||||
| Sprint: | Query 12 (04/04/16), Query 13 (04/22/16), Query 15 (06/03/16), Query 17 (07/15/16) | ||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||
| Case: | (copied to CRM) | ||||||||||||||||||||||||||||
| Description |
|
Syntax
Examples
Notes
Errors
Old Description Seems painful to fall back to map/reduce solely because someone saves a number or date as a string |
| Comments |
| Comment by Charlie Swanson [ 06/Apr/18 ] | ||||||||||||
|
As a work around in 3.6, you could consider using a collation with numericOrdering: true for that particular issue. See the docs for collation Hope that helps. | ||||||||||||
| Comment by Ankit Rathore [ 05/Apr/18 ] | ||||||||||||
|
is there any work around for this in the current mongo version 3.6 , till this fix released. i am struggling with this issue . Any help will be much appreciated. i need to select converted integer value from a string in '$project' stage and i have to compare this value in '$match' stage for $gt and $lt comparison. for example , empName:'$name'}, }} | ||||||||||||
| Comment by Justin Seyster [ 06/Mar/18 ] | ||||||||||||
|
We now have $toInt, $toString, etc. conversion operators in aggregation ( | ||||||||||||
| Comment by Charlie Swanson [ 05/Jul/16 ] | ||||||||||||
|
asya, my hunch is no. If you put a default that's not an integer we'd assume you know what you're doing? In particular, I believe adinoyi.omuya was considering using a default of null to allow distinguishing those that did not convert cleanly? | ||||||||||||
| Comment by Asya Kamsky [ 02/Jul/16 ] | ||||||||||||
|
Will we error if default is not an integer? Like null for instance? | ||||||||||||
| Comment by Eliot Horowitz (Inactive) [ 01/Jul/16 ] | ||||||||||||
|
I'm ok with the suggestion above. | ||||||||||||
| Comment by Charlie Swanson [ 30/Jun/16 ] | ||||||||||||
|
adinoyi.omuya has pointed out that it would be useful to have an optional default specified, as well as coerceToFoo versions which are more lenient. Something like the following:
I can think of a couple alternatives worth exploring:
That said, I think the proposal above (in the example code) is still the best way forward. asya, david.storch, eliot, thoughts? | ||||||||||||
| Comment by Matt Kangas [ 02/Jun/15 ] | ||||||||||||
|
Today at MongoDB World nuha requested "atoi" or similar functionality in the agg framework. | ||||||||||||
| Comment by Mathias Stearn [ 17/Feb/15 ] | ||||||||||||
|
While that works, I think that is best considered an "unintentional feature" (ie a bug). Either way, I think it is separately worth adding a real, explicit way to stringify a number similar to $dateToString. | ||||||||||||
| Comment by Asya Kamsky [ 17/Feb/15 ] | ||||||||||||
|
it turns out both 2.6 and 3.0 allow using $substr to convert a number to a string.
|