Reuben L.
Reuben L.

Reputation: 2859

mongoexport returning scientific (exponential) notation

So I was exporting some collections using mongoexport and found cases where large integers such as 9999999999999 were exported as 9.9999999999999e+13

So two questions: Why is this the case, and how can I prevent this?

Thank you.

Upvotes: 5

Views: 2247

Answers (2)

Alok
Alok

Reputation: 10618

For dumping data you can use --eval option of mongo command line client and jq command. That does not give data in scientific notation.

for exmaple

mongo --quiet localhost:5010/user  --username alok --password alok --authenticationDatabase admin --eval 'db.users.find({}, {_id: 1, username:1, name: 1}).limit(10).toArray().map(JSON.stringify).join("\n")' | while read item; do echo $(echo $item | jq ._id),$(echo $item | jq .username),$(echo $item | jq .name); done > data.csv

Upvotes: 0

Ghislain Fourny
Ghislain Fourny

Reputation: 7279

MongoDB treats all number literals as floating point by default, and above a certain threshold (32 bits?) it switches to scientific notation when exporting to JSON.

Upvotes: 3

Related Questions