Skip to content Skip to sidebar Skip to footer

Python - Pymongo Mongodb 3.4 - Numberdecimal

i am using mongodb 3.4 in order to insert proper prices to my database.. According to: https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst#r

Solution 1:

Simply this:

collection.insert_one({'credits': Decimal128('9.99')})

(The "$numberDecimal" syntax is for expressing a decimal number within a JSON string. But you're not using JSON at all.)

Solution 2:

The link you provided tells you about reading from extended JSON. If you want to insert it as a decimal, you should just use:

{ 'credits': '{0:.2f}'.format(9.99) }

The '{0:.2f}'.format(9.99) part will format the number you pass (in this case 9.99) to include 2 decimals every time. So '{0:.2f}'.format(9) should result in 9.00 in your database.

Post a Comment for "Python - Pymongo Mongodb 3.4 - Numberdecimal"