mongo OverflowError: MongoDB can only handle up to 8-byte ints

在尝试使用Python的pymongo库向MongoDB存储超过64位整数时遇到了OverflowError。MongoDB仅支持最大8字节(64位)的整数,导致128位整数无法存储。解决方案是确保插入的数据不超过MongoDB的Int64限制,即-9223372036854775808到9223372036854775807的范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

____tz_zs

在使用 python 往 mongo 中存储数据时,抛出OverflowError: MongoDB can only handle up to 8-byte ints错误。

from pymongo import MongoClient

# 地址,端口
mongo_client = MongoClient("ip", <端口号>)  # 创建 MongoClient 对象
mongo_db = mongo_client["数据库名"]  # MongoDB 中可存在多个数据库,根据数据库名称获取数据库对象(Database)
mongo_db.authenticate("用户", "密码")  # 登录认证

record = {
    "_id": "long_int_test",
    "int": 11111111111111111111111111111111111111111111111111
}
res = mongo_db["test_table"].replace_one(filter={"_id": record["_id"]}, replacement=record, upsert=True)
print(res)

"""
OverflowError: MongoDB can only handle up to 8-byte ints
"""

这是因为 python 的 int 类型是长整型,可以在计算中处理任意大的整数。而 MongoDB 的原生二进制扩展 JSON 格式/数据类型 —— 仅支持 32 位(有符号)和 64 位(有符号)整数 —— 8 字节为 64 位。

所以可以存储在 64 位 int 中的最大整数值是:
9,223,372,036,854,775,807

---
Int32有符号32位整数-2147483648-2147483647
UInt32无符号32位整数0~4294967295
Int64有符号64位整数-9223372036854775808~9223372036854775807
UInt64无符号64位整数0~18446744073709551615

参考:
OverflowError: MongoDB can only handle up to 8-byte ints?

为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值