MongoDB converts number to string in scientific notation
Asked Answered
T

2

5

I want to get full number as a String, but instead "1490650000000" it returns scientific notation "1.49065e+12"

Here's how I try to convert it:

{$substr: ["$myNumber", 0, -1]};

Any ideas how to prevent it?

Note: I'm using v3.6 and can't upgrade to use $toString (thanks mlab).

Tieshatieup answered 8/7, 2018 at 10:8 Comment(0)
L
10

You can try as below.

db.collectionName.aggregate([
{ $addFields: { "strFld": {$toString: {$toLong:"$numberFiled"}}} },
])
Lozano answered 4/10, 2019 at 14:36 Comment(0)
G
1

If you have mongodb 4.0 then you can use $toLong aggregation

db.collection.aggregate([
  {
    "$project": {
      "myNumber": {
        "$toLong": "$myNumber"
      }
    }
  }
])
Gasket answered 8/7, 2018 at 10:34 Comment(2)
"Also I'm using v3.6 and can't update to use $toString (thanks mlab)." Unfortunately I can't use 4.0Tieshatieup
Then you have to do this with JavaScript because there is no operator till 4.0 which can convert number to stringGasket

© 2022 - 2024 — McMap. All rights reserved.