#-*-Coding:utf-8-*-' Python MONGO operation Demodone: ' from Pymongo import mongoclientconn = Nonetopics = Noneusers=nonedef p Rocess (): connection=mongoclient (' localhost ', 27017) Global Conn conn = connection; #列出server_info信息 #print conn.server_info () #列出全部数据库 databases = Conn.database_names () print databases topics = Conn.node. Topics Users=conn.node.users Ret=users.find ({' LoginName ': ' Dingxiaoyuehao '}) if Ret and Ret.count () > 0:print "Yes" El Se:print "No" #删除库和表 #dropTable () #添加数据库lifeba及表 (collections) Users #createTable () #插入数据 #insertDatas () #更新数据 # UpdateData () #查询数据 #queryData () #删除数据 #deleteData () #释放连接 #dbconn. Close () def insertdatas (): datas=[{"name": "Steven1", " Realname ":" Test 1 "," age ": [+], {" Name ":" Steven2 "," realname ":" Test 2 "," Age ": +}, {" Name ":" Steven1 "," realname ":" Test 3 "," Age ": Topics.insert" (datas) def updatedata (): "Only modify the last match to the data 3rd parameter is set to True, the data is not found to add a 4th parameter set to True, there are multiple records do not update" Topics.update ({' name ': ' Steven1 '},{' $set ': {' realname ': ' Test 1 Modified '}}, False,false) def deleteData (): ToPics.remove ({' name ': ' Steven1 '}) def querydata (): #查询全部数据 rows = Topics.find () printresult (rows) #查询一个数据 print Topics.find_one () #带条件查询 printresult ({' name ': ' Steven2 '}) Printresult (Topics.find ({' name ': {' $gt ': 25}} ) def createtable (): ' Create library and table ' ' Global topics topics = Conn.lifeba.topicsdef droptable (): ' Delete table ' ' Global conn Conn.drop _database ("Lifeba") def printresult (rows): For row in Rows:for key in Row.keys (): #遍历字典 print Row[key].encode (' gb2312 '), #加 , do not wrap print "if __name__ = = ' __main__ ': Process ()
Python MONGO operation