Writing big unsigned int64 to mongoDB with pymogno

空扰寡人 提交于 2020-01-03 04:52:07

问题


I have a number I'm getting from an external source I want to write to the mongo as Int

The problem is that the number is above signed Int64 (which is how mongo implements it) - but is still fits unsinged int64

The number is: 9223372036854776000

How can I still write it to mongo? I'm getting OverflowError: MongoDB can only handle up to 8-byte ints


回答1:


With the latest PyMongo and MongoDB 3.4 or later, you can use the standard Python Decimal with the new BSON Decimal128 type.

from decimal import Decimal
from bson.decimal128 import Decimal128
from pymongo import MongoClient

d = Decimal128(Decimal(9223372036854776000))
collection = MongoClient().test.test
collection.insert_one({'myNumber': d})


来源:https://stackoverflow.com/questions/43875204/writing-big-unsigned-int64-to-mongodb-with-pymogno

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