loadmongo.py 2.0 KB

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