import xml.etree.ElementTree as ET from subdir import service from bson.objectid import ObjectId from pymongo import AsyncMongoClient import asyncio import uuid client = AsyncMongoClient() async def resolve(data): db = client["baojia"] collection = db["jingjibiao"] document = await collection.find_one({'_id': ObjectId(data)}) dxgc = document['Dxgcxx'] result = [] for entry in dxgc: newone = {"名称" : entry['Dxgcmc'], "工程类型":"", "工程类别": "", "管理费(%)": "", "利润(%)": "", "备注":"", "key" : str(uuid.uuid4()) } children = [] for dwgc in entry['Dwgc']: children.append({"名称" : dwgc['Dwgcmc'],"工程类型":"", "工程类别": "", "管理费(%)": "", "利润(%)": "","备注":"", "key" : dwgc['Dwgcbh']}) newone["children"] = children result.append(newone) collection = db["qufei"] await collection.insert_one({"biao_id": data, "qufei": [{ "名称" : document['Xmmc'], "工程类型":"", "工程类别": "", "管理费(%)": "", "利润(%)": "", "备注":"", "key" : str(uuid.uuid4()), "children" : result }]}) import os ttt = ['6870b7ce960253992a3d5cf0', '6870b7e467d3b6b24590afd6', '6870b7ed267357fc535650a6'] asyncio.run(resolve(ttt[2]))