问题描述
我有一个要从外部来源获得的号码,我想以Int的形式写到mongo
I have a number I'm getting from an external source I want to write to the mongo as Int
问题是该数字高于带符号的Int64(这是mongo的实现方式)-但仍然适合未命名的int64
The problem is that the number is above signed Int64 (which is how mongo implements it) - but is still fits unsinged int64
电话号码是:9223372036854776000
The number is:9223372036854776000
我如何仍将其写入mongo?我越来越OverflowError: MongoDB can only handle up to 8-byte ints
How can I still write it to mongo?I'm getting OverflowError: MongoDB can only handle up to 8-byte ints
推荐答案
使用最新的PyMongo和MongoDB 3.4或更高版本,您可以将标准的Python Decimal与新的BSON Decimal128类型一起使用.
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})
这篇关于使用pymogno将大的未签名的int64写入mongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!