python連接mongodb數(shù)據(jù)庫操作數(shù)據(jù)示例
更新時間:2020年11月30日 17:32:56 作者:WAYAHA
這篇文章主要介紹了python連接mongodb操作數(shù)據(jù)示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
作者: wyh草樣
1、數(shù)據(jù)庫配置類 MongoDBConn.py
#encoding=utf-8
'''
Mongo Conn連接類
'''
import pymongo
class DBConn:
conn = None
servers = "mongodb://localhost:27017"
def connect(self):
self.conn = pymongo.Connection(self.servers)
def close(self):
return self.conn.disconnect()
def getConn(self):
return self.conn
2、ngoDemo.py 類
#encoding=utf-8
'''
Mongo操作Demo
Done:
'''
import MongoDBConn
dbconn = MongoDBConn.DBConn()
conn = None
lifeba_users = None
def process():
#建立連接
dbconn.connect()
global conn
conn = dbconn.getConn()
#列出server_info信息
print conn.server_info()
#列出全部數(shù)據(jù)庫
databases = conn.database_names()
print databases
#刪除庫和表
dropTable()
#添加數(shù)據(jù)庫lifeba及表(collections)users
createTable()
#插入數(shù)據(jù)
insertDatas()
#更新數(shù)據(jù)
updateData()
#查詢數(shù)據(jù)
queryData()
#刪除數(shù)據(jù)
deleteData()
#釋放連接
dbconn.close()
def insertDatas():
datas=[{"name":"steven1","realname":"測試1","age":25},
{"name":"steven2","realname":"測試2","age":26},
{"name":"steven1","realname":"測試3","age":23}]
lifeba_users.insert(datas)
def updateData():
'''只修改最后一條匹配到的數(shù)據(jù)
第3個參數(shù)設(shè)置為True,沒找到該數(shù)據(jù)就添加一條
第4個參數(shù)設(shè)置為True,有多條記錄就不更新
'''
lifeba_users.update({'name':'steven1'},{'$set':{'realname':'測試1修改'}}, False,False)
def deleteData():
lifeba_users.remove({'name':'steven1'})
def queryData():
#查詢?nèi)繑?shù)據(jù)
rows = lifeba_users.find()
printResult(rows)
#查詢一個數(shù)據(jù)
print lifeba_users.find_one()
#帶條件查詢
printResult(lifeba_users.find({'name':'steven2'}))
printResult(lifeba_users.find({'name':{'$gt':25}}))
def createTable():
'''創(chuàng)建庫和表'''
global lifeba_users
lifeba_users = conn.lifeba.users
def dropTable():
'''刪除表'''
global conn
conn.drop_database("lifeba")
def printResult(rows):
for row in rows:
for key in row.keys():#遍歷字典
print row[key], #加, 不換行打印
print ''
if __name__ == '__main__':
process()
以上就是python連接mongodb數(shù)據(jù)庫操作數(shù)據(jù)示例的詳細(xì)內(nèi)容,更多關(guān)于python連接mongodb的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
- python連接mongodb操作數(shù)據(jù)示例(mongodb數(shù)據(jù)庫配置類)
- Python中的MongoDB基本操作:連接、查詢實例
- python連接mongodb密碼認(rèn)證實例
- python連接MySQL、MongoDB、Redis、memcache等數(shù)據(jù)庫的方法
- Python簡單連接MongoDB數(shù)據(jù)庫的方法
- python實現(xiàn)連接mongodb的方法
- python連接mongodb集群方法詳解
- python連接、操作mongodb數(shù)據(jù)庫的方法實例詳解
- Mongodb基本操作與Python連接mongodb并進(jìn)行基礎(chǔ)操作的方法
- Python如何使用pymongo連接MongoDB數(shù)據(jù)庫并進(jìn)行相關(guān)操作
相關(guān)文章
詳解Python如何利用Shelve進(jìn)行數(shù)據(jù)存儲
Shelve是Python標(biāo)準(zhǔn)庫中的一個模塊,用于實現(xiàn)簡單的數(shù)據(jù)持久化,本文將詳細(xì)介紹Shelve模塊的功能和用法,并提供豐富的示例代碼,希望對大家有所幫助2023-11-11
Python capitalize()函數(shù)的用法詳解
在Python中,capitalize()將字符串的第一個字符轉(zhuǎn)換為大寫字母,并將所有其他字符(如果有的話)轉(zhuǎn)換為小寫,本文就將給大家介紹一下Python capitalize()函數(shù)的使用方法,感興趣的朋友跟著小編一起來看看吧2023-07-07
Pandas中的unique()和nunique()區(qū)別詳解
Pandas中Series和DataFrame的兩種數(shù)據(jù)類型中都有nunique()和unique()方法,本文詳細(xì)的介紹了兩者的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2022-08-08

