Python Operation MongoDB

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.