OverflowError:MongoDB最多只能处理8字节的整数? [英] OverflowError: MongoDB can only handle up to 8-byte ints?
问题描述
最近12个小时我一直在网上搜索.我完全迷路了,请帮忙.
I have spent the last 12 hours scouring the web. I am completely lost, please help.
我正在尝试从API端点提取数据并将其放入MongoDB.数据如下:
I am trying to pull data from an API endpoint and put it into MongoDB. The data looks like this:
{"_links": {
"self": {
"href": "https://us.api.battle.net/data/sc2/ladder/271302?namespace=prod"
}
},
"league": {
"league_key": {
"league_id": 5,
"season_id": 37,
"queue_id": 201,
"team_type": 0
},
"key": {
"href": "https://us.api.battle.net/data/sc2/league/37/201/0/5?namespace=prod"
}
},
"team": [
{
"id": 6956151645604413000,
"rating": 5321,
"wins": 131,
"losses": 64,
"ties": 0,
"points": 1601,
"longest_win_streak": 15,
"current_win_streak": 4,
"current_rank": 1,
"highest_rank": 10,
"previous_rank": 1,
"join_time_stamp": 1534903699,
"last_played_time_stamp": 1537822019,
"member": [
{
"legacy_link": {
"id": 9964871,
"realm": 1,
"name": "mTOR#378",
"path": "/profile/9964871/1/mTOR"
},
"played_race_count": [
{
"race": "Zerg",
"count": 195
}
],
"character_link": {
"id": 9964871,
"battle_tag": "Hellghost#11903",
"key": {
"href": "https://us.api.battle.net/data/sc2/character/Hellghost-11903/9964871?namespace=prod"
}
}
}
]
},
{
"id": 11611747760398664000, .....
....
这是代码:
for ladder_number in ladder_array:
ladder_call_url = ladder_call+slash+str(ladder_number)+eng_locale+access_token
url = str(ladder_call_url)
response = requests.get(url)
print('trying ladder number '+str(ladder_number))
print('calling :'+url)
if response.status_code == 200:
print('status: '+str(response))
mmr_db.ladders.insert_one(response.json())
我得到一个错误:
OverflowError:MongoDB最多只能处理8个字节的整数?
OverflowError: MongoDB can only handle up to 8-byte ints?
这是因为我要加载的数据太大吗? "ID"整数是否太大?
Is this because the data I am trying to load is too large? Are the "ID" integers too large?
哦,衷心感谢您的帮助.
Oh man, any help would be sincerely appreciated.
_______编辑____________
_______ EDIT ____________
经过编辑以包括回溯":
Edited to include the Traceback:
Traceback (most recent call last):
File "C:\scripts\mmr_from_ladders.py", line 96, in <module>
mmr_db.ladders.insert_one(response.json(), bypass_document_validation=True)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 693, in insert_one
session=session),
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 607, in _insert
bypass_doc_val, session)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 595, in _insert_one
acknowledged, _insert_command, session)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\mongo_client.py", line 1243, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\mongo_client.py", line 1196, in _retry_with_session
return func(session, sock_info, retryable)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 590, in _insert_command
retryable_write=retryable_write)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\pool.py", line 584, in command
self._raise_connection_failure(error)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\pool.py", line 745, in _raise_connection_failure
raise error
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\pool.py", line 579, in command
unacknowledged=unacknowledged)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\network.py", line 114, in command
codec_options, ctx=compression_ctx)
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\message.py", line 679, in _op_msg
flags, command, identifier, docs, check_keys, opts)
OverflowError: MongoDB can only handle up to 8-byte ints
推荐答案
BSON规范 — MongoDB的本机二进制扩展JSON格式/数据类型-仅支持32位(带符号)和64位(带符号)整数-8个字节(即64位).
The BSON spec — MongoDB’s native binary extended JSON format / data type — only supports 32 bit (signed) and 64 bit (signed) integers — 8 bytes being 64 bits.
可以存储在64位int中的最大整数值为: 9,223,372,036,854,775,807
The maximum integer value that can be stored in a 64 bit int is: 9,223,372,036,854,775,807
在您的示例中,您似乎具有较大的ID,例如: 11,611,747,760,398,664,000
In your example you appear to have larger ids, for example: 11,611,747,760,398,664,000
我猜测生成此数据的应用程序正在使用uint64类型(未签名的可以容纳x2-1值).
I’m guessing that the app generating this data is using uint64 types (unsigned can hold x2-1 values).
如果可能的话,我将首先研究以下两种可能的解决方案:
I would start by looking at either of these potential solutions, if possible:
- 更改另一端以将int64(带符号)类型用于ID.
- 然后使用 ObjectId()替换传入的ID获取12个字节的〜GUID作为您的唯一ID.
- Changing the other side to use int64 (signed) types for the IDs.
- Replacing the incoming IDs using ObjectId() as you then get a 12 byte ~ GUID for your unique IDs.
这篇关于OverflowError:MongoDB最多只能处理8字节的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!