Mongodb from v.3.4 supports NumberDecimal
I have MongoDB server version: 3.4.1, but when I execute mongoimport shell command I get an error:
Failed: error processing document #7: invalid character 'D' in literal NumberInt or NumberLong (expecting 'I' or 'L')
My json:
{"c":"7E474601-B511-4AD9-B2B8-7E61807F9673","n":"n1","v":NumberDecimal("95")}
Normal insert executes ok, so this is mongoimort problem.
db.aaa.insert({"c":"7E474601-B511-4AD9-B2B8-7E61807F9673","n":"n1","v":NumberDecimal("95")})
WriteResult({ "nInserted" : 1 })
Why is that? Can you help me?
One trick to figure out the correct extended json format is to use the mongoexport utility on a collection to see how MongoDB itself publishes the json out.
In this case the following should work:
{"c":"7E474601-B511-4AD9-B2B8-7E61807F9673","n":"n1","v":{"$numberDecimal":"95"}}
I tested this with mongoimport with server version 3.4.1 and it imports the numberDecimal correctly.
NumberDecimal("95") is not a valid JSON representation. It should be
{"c":"7E474601-B511-4AD9-B2B8-7E61807F9673","n":"n1","v":95}
It works with mongo shell because the shell is a javascript interpreter that treats NumberDecimal as a valid data type.
If you need NumberDecimal data type you need to use a driver or the mongo shell.
Form a bash shell you can run this simple script:
for i in `cat aaa.json`
do
mongo --quiet --eval "db.aaa.insert($i)"
done
Please note that this is row by row insertion and not very efficient.