Import Pymongo
Import CSV
Import OS
Import datetime
Class Device1 ():
Def conn (self):
Global Connetion
Connetion=pymongo. Mongoclient (' 10.10.5.199 ', 27017)
def find_db (Self,dbname1,table1,table2,path):
Db1=connetion.get_database (dbname1) # #NGTenantTemplate Databse
Device_table=db1.get_collection (table1)
Devicetype_table=db1.get_collection (table2)
_id= ' _id '
Typename= ' TypeName '
Maintype= ' MainType '
Vendor= ' Vendor '
Action= ' action '
Enable= ' Enable '
Parserorder= ' ParserOrder '
Name= ' name '
path1=path+ "\\chengpu.csv"
Csvfile=open (path1, ' W ', newline= ')
Writer=csv.writer (CSVFile)
Writer.writerow ([' ID ', ' Device Type ', ' Category ', ' Default Vendor ', ' support operation ', ' Status ', ' Parse Order '])
Device_data1=device_table.find ({},{_id: ' true ', TypeName: ' True ', MainType: ' True ', Vendor: ' True ', action: ' True ', Enable: ' True ', ParserOrder: ' true '})
For data in Device_data1:
ID=DATA[_ID]
Devicetype=data[typename]
Devicetype_data=devicetype_table.find_one ({_id:data[maintype]},{name:1})
Category=devicetype_data[name]
Defaultvendor=data[vendor]
List_action=data[action]
Supportoperation0=[]
supportoperation1={}
Supportoperation2=[]
For I in List_action:
if (i[' actionenable ']==true):
Supportoperation0.append (i[' ActionType ')
supportoperation1[i[' ActionType ']]= ' (1) '
Else
Supportoperation0.append (i[' ActionType ')
supportoperation1[i[' ActionType ']]= ' (0) '
Supportoperation0.sort ()
For K in SupportOperation0:
Supportoperation2.append (str (k) +supportoperation1[k])
Supportoperation= ' | '. Join (SupportOperation2)
Status0=data[enable]
if (status0==true):
status= ' Enable '
Else
status= ' Disable '
Parseorder=data[parserorder]
Da=[id,devicetype,category,defaultvendor,supportoperation,status,parseorder]
#print (DA)
Writer.writerow (DA)
If __name__== "__main__":
Path= ' D:\\SEARCH\\AA\\BB '
Print ("Starting download data from Mongodb,please wait ...")
Datetime1=datetime.datetime.now ()
dbname1= ' Ngtenanttemplate '
table1= ' DeviceType '
Table2= ' Devicemaintype '
De=device1 ()
De.conn ()
de.find_db (Dbname1,table1,table2,path)
Datetime2=datetime.datetime.now ()
Print ("done! Download data cost: "+str ((datetime2-datetime1)))
Python Operation MongoDB