使用pymogno将大的未签名的int64写入mongoDB [英] Writing big unsigned int64 to mongoDB with pymogno

查看:129
本文介绍了使用pymogno将大的未签名的int64写入mongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要从外部来源获得的号码,我想以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或更高版本,

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆