MongoDB converts number to string in scientific notation

半城伤御伤魂 提交于 2021-01-28 05:00:20

问题


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).


回答1:


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

db.collection.aggregate([
  {
    "$project": {
      "myNumber": {
        "$toLong": "$myNumber"
      }
    }
  }
])



回答2:


You can try as below.

db.collectionName.aggregate([
{ $addFields: { "strFld": {$toString: {$toLong:"$numberFiled"}}} },
])


来源:https://stackoverflow.com/questions/51230909/mongodb-converts-number-to-string-in-scientific-notation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!