Boaz
Boaz

Reputation: 5084

Writing big unsigned int64 to mongoDB with pymongo

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

Upvotes: 1

Views: 1671

Answers (1)

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24017

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})

Upvotes: 1

Related Questions