首页 > 代码库 > python操作mongodb
python操作mongodb
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=http://www.mamicode.com/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操作mongodb
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。