[SERVER-47200] how to compare two NumberDecimal digits in javascript Created: 31/Mar/20 Updated: 27/Oct/23 Resolved: 31/Mar/20 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Question | Priority: | Major - P3 |
| Reporter: | winnie wu | Assignee: | Dmitry Agranat |
| Resolution: | Community Answered | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||
| Backwards Compatibility: | Fully Compatible | ||||
| Participants: | |||||
| Linked BF Score: | 15 | ||||
| Description |
|
Hi, I am using map-reduce and in the map function I am trying to find out all objects with its value = or > or < a given decimal value. those objects are all NumberDecimal ones. note for some reason, I can't use aggregation operation. My question is: how can I compare two NumberDecimal digits in Javascripts? Does Mongo provide a function like compareTo() that I can use? e.g var X=new NumberDecimal("100"); in JavaScript, X== Y is false. Perhaps I should converse the two to BigDecimal type and then do comparation? As I know, JavaScript doesn't have a native Decimal Type, and I have to use a library which define BigDecimal. Is it possible for to use this library in map-reduce operation? How should achieve this? by the way, I found the a lib who defines the bigDecimal : https://github.com/royNiladri/js-big-decimal#readme but it requires me to install it like this: npm install --save js-big-decimal and then introduce it like this: var bigDecimal = require('js-big-decimal'); but I think I can't install it in mongo server. it also suggest for web , I can do like this: <script src="node_modules/js-big-decimal/dist/web/js-big-decimal.min.js"></script> but I don't think It is applicable for me. Is there any good suggestion for me to achieve my goal? Note I need to compare the two digits by <, =, and >.
|
| Comments |
| Comment by Dmitry Agranat [ 31/Mar/20 ] | ||||||
|
Being able to compare different decimal128 values to each other has come up for testing the database as well. We use the bsonWoCompare(obj1, obj2) function that's available in mongo shell to do so. That same function is also available in the server-side JavaScript environment with $where and mapReduce. It will return
Some examples:
Note that if you're using a recent version of MongoDB (4.0.4 or later), then you don't actually need to wrap the values in {_: <value>} objects as a result of the changes from The SERVER project is for bugs and feature suggestions for the MongoDB server. As this ticket does not appear to be a bug, I will now close it. If you need further assistance troubleshooting, I encourage you to ask our community by posting on the MongoDB Community Forums or on Stack Overflow with the mongodb tag. Regards, |