python列表按照批次分配数据

# 场景:一次取回mongodb某表-64万数据,pymongo.errors.DocumentTooLarge: BSON document too large (28888095 bytes) - 
# the connected server supports BSON document sizes up to 16777216 bytes. 文档太大就无法返回,因此分10次取,每次
# 取回6400条,然后统一更新到本地的字典中。

import uuid

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 5, 6, 7, 8, 9, 6, 5, 4, 3, 8, 9]
batch_step = round(len(data)/10)

for index in range(0, len(data), batch_step):
    item_list = data[index:index+batch_step]
    
# example
from pymongo import MongoClient
mdb = MongoClient('120.133.26.xxx:20002', username='xt', password='xxxxxx')
image_ids = ["001", "002", "003", ...]

image_dict = {}
batch_step = round(len(image_ids)/10)
for idx in range(0, len(image_ids), batch_step):
    image_ids_part = image_ids[idx:idx + batch_step]
    image_infos = mdb['数据库名']['图片表名'].find({"image_id": {"$in": image_ids_part}})

    image_one = {}
    for image_info in image_infos:
        if image_info.get("image_size"):
            image_one[image_info.get("image_id")] = image_info
            image_dict.update(image_one)

  

转载于:https://www.cnblogs.com/adamans/articles/10500782.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值