loadmongo.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import pymongo
  2. import pandas
  3. import json
  4. from pymongo import MongoClient
  5. client = MongoClient()
  6. db = client["dinge"]
  7. collection = db["de-collection"]
  8. from subdir import service
  9. ##print(collection.find_one({"DEBH": "3-94"}))
  10. def handle_cl(cl):
  11. result = {}
  12. result["CLBH"] = cl["CLBH"]
  13. result["CLMC"] = cl["CLMC"]
  14. result["JLDW"] = cl["JLDW"]
  15. result["YSJG"] = cl["YSJG"]
  16. result["SL"] = cl["SL"]
  17. result["HJ"] = cl["HJ"]
  18. return result
  19. def handle_rg(rg):
  20. result = {}
  21. result["CLBH"] = rg["CLBH"]
  22. result["CLMC"] = rg["CLMC"]
  23. result["JLDW"] = rg["JLDW"]
  24. result["YSJG"] = rg["YSJG"]
  25. result["gr"] = rg["gr"]
  26. result["gf"] = rg["gf"]
  27. return result
  28. def handle_jx(jx):
  29. result = {}
  30. result["jxbh"] = jx["jxbh"]
  31. result["jxmc"] = jx["jxmc"]
  32. result["DW"] = jx["DW"]
  33. result["tbdj"] = jx["tbdj"]
  34. result["sl"] = jx["sl"]
  35. result["hj"] = jx["hj"]
  36. return result
  37. for post in collection.find({"zhuanye": "修缮工程安装"}):
  38. myquery = { "zhuanye": "修缮工程土建安装",
  39. "DEBH": post["DEBH"]
  40. }
  41. A1, A2, rg, jx, cl, bz_ = service.getSingleDeXilie_xsaz(post["DEBH"])
  42. if bz_:
  43. bz = json.loads(bz_)
  44. mongo_bz = []
  45. for key in bz["ID"].keys():
  46. mongo_bz.append({
  47. "BZBH" : bz["BZBH"][key],
  48. "SM": bz["SM"][key],
  49. "THMC" : bz["THMC"][key]
  50. })
  51. else:
  52. mongo_bz = []
  53. mongo_rg = [handle_rg(entry) for entry in rg ]
  54. mongo_jx = [handle_jx(entry) for entry in jx ]
  55. mongo_cl = [handle_cl(entry) for entry in cl ]
  56. newvalues = { "$set": { "mongo_bz": mongo_bz,
  57. "mongo_rg": mongo_rg,
  58. "mongo_cl": mongo_cl,
  59. "mongo_jx": mongo_jx
  60. } }
  61. collection.update_one(myquery, newvalues)