OverflowError:MongoDB最多只能处理8字节的整数?

时间:2018-09-27 20:17:36

标签: python json mongodb

最近12个小时我一直在网上搜索。我完全迷路了,请帮忙。

我正在尝试从API端点提取数据并将其放入MongoDB。数据如下:

{"_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个字节的整数?

这是因为我要加载的数据太大吗? “ ID”整数是否太大?

哦,衷心感谢您的帮助。

_______编辑____________

经过编辑以包括“追溯”:

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

1 个答案:

答案 0 :(得分:3)

BSON spec(MongoDB的本机二进制扩展JSON格式/数据类型)仅支持32位(带符号)和64位(带符号)整数(8个字节为64位)。

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

在您的示例中,您似乎具有更大的ID,例如: 11,611,747,760,398,664,000

我猜测生成此数据的应用程序正在使用uint64类型(无符号可以容纳x2-1值)。

如果可能的话,我将首先研究以下两种可能的解决方案:

  • 更改另一端以将int64(带符号)类型用于ID。
  • 使用ObjectId()替换传入的ID,然后获得12字节〜GUID作为唯一ID。