好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 如何用python 操作MongoDB數(shù)據(jù)庫

如何用python 操作MongoDB數(shù)據(jù)庫

熱門標(biāo)簽:打印谷歌地圖標(biāo)注 京華圖書館地圖標(biāo)注 佛山通用400電話申請 看懂地圖標(biāo)注方法 蘇州人工外呼系統(tǒng)軟件 廣東旅游地圖標(biāo)注 電話機(jī)器人貸款詐騙 電話外呼系統(tǒng)招商代理 淮安呼叫中心外呼系統(tǒng)如何

一、前言

  MongoDB屬于 NoSQL(非關(guān)系型數(shù)據(jù)庫),是一個(gè)基于分布式文件存儲的開源數(shù)據(jù)庫系統(tǒng)。

二、操作 MongoDB

1、安裝 pymongo

python 使用第三方庫來連接操作 MongoDB,所以我們首先安裝此庫。

pip3 install pymongodb

2、連接 MongoDB

使用 MongoClient 類連接,以下兩種參數(shù)方式都可以:

from pymongo import MongoClient

# 連接方式一
client = MongoClient(host='localhost',port=27017)
# 連接方式二
# client = MongoClient('mongodb://localhost:27017/')

3、選擇數(shù)據(jù)庫

MongoDB 可以創(chuàng)建很多 db,指定我們需要的 db 即可

# 方式一
db = client.Monitor
# 方式二
# db = client['Monitor']

4、選擇集合

db 內(nèi)包含很多個(gè)集合,有點(diǎn)類似 mysql 這類關(guān)系型數(shù)據(jù)庫中的表

# 方式一
collection = db.test
# 方式二
# collection = db['test']

5、插入數(shù)據(jù)

插入一條數(shù)據(jù),MongoDB 每條記錄都有一個(gè)唯一標(biāo)識。返回一個(gè) InsertOneResult 對象,若需要獲取唯一標(biāo)識,找到 InsertOneResult 對象的屬性 inserted_id 即可

from pymongo import MongoClient

class mongodb:
    def __init__(self,host,db,port = 27017):
        '''
        :param host: str mongodb地址
        :param db: str 數(shù)據(jù)庫
        :param port: int 端口,默認(rèn)為27017
        '''
        host = host
        db = db
        self.port = port
        client = MongoClient(host=host,port=port)
        self.db = client[db]

    def insert_one(self,table,dic):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param dic: dict 要插入的字典
        :return: 返回一個(gè)包含ObjectId類型的對象
        '''
        collection = self.db[table]
        rep = collection.insert_one(dic)

        return repif __name__=='__main__':
    dic = {'姓名':'小明','English':100,'math':90}
    db = mongodb(host='localhost',db = 'test')
    rep = db.insert_one('test',dic)
    print(rep.inserted_id)

插入多條數(shù)據(jù),使用 insert_many 批量插入

from pymongo import MongoClient

class mongodb:
    def __init__(self,host,db,port = 27017):
        '''
        :param host: str mongodb地址
        :param db: str 數(shù)據(jù)庫
        :param port: int 端口,默認(rèn)為27017
        '''
        host = host
        db = db
        self.port = port
        client = MongoClient(host=host,port=port)
        self.db = client[db]

    def insert_one(self,table,dic):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param dic: dict 要插入的字典
        :return: 返回包含一個(gè)ObjectId類型的對象
        '''
        collection = self.db[table]
        rep = collection.insert_one(dic)

        return rep

    def insert_many(self,table,lists):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param dic: dict 要插入的列表,列表中的元素為字典
        :return: 返回包含多個(gè)ObjectId類型的列表對象
        '''
        collection = self.db[table]
        rep = collection.insert_many(lists)

        return rep


if __name__=='__main__':
    lists = [{'姓名':'小明','English':100,'math':90},
             {'姓名':'小華','English':90,'math':100}]
    db = mongodb(host='localhost',db = 'test')
    rep = db.insert_many('test',lists)
    for i in rep.inserted_ids:
        print(i)

6、查詢

1)常規(guī)查詢

  •  find_one :查詢單條記錄,返回一個(gè)字典。
  •  find:查詢多條記錄 ,返回一個(gè)游標(biāo)對象。
from pymongo import MongoClient

class mongodb:
    def __init__(self,host,db,port = 27017):
        '''
        :param host: str mongodb地址
        :param db: str 數(shù)據(jù)庫
        :param port: int 端口,默認(rèn)為27017
        '''
        host = host
        db = db
        self.port = port
        client = MongoClient(host=host,port=port)
        self.db = client[db]

    def find_one(self,table,dic):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param dic: dict 查詢條件
        :return: dict 返回單條記錄的字典
        '''
        collection = self.db[table]
        rep = collection.find_one(dic)

        return rep

    def find(self,table,dic):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param dic: dict 查詢條件
        :return: list 返回查詢到記錄的列表
        '''
        collection = self.db[table]
        rep = list(collection.find(dic))

        return rep

if __name__=='__main__':
    # 查詢 English 成績?yōu)?100 的所有記錄
    dic = {'English':100}
    db = mongodb(host='localhost',db = 'test')
    rep = db.insert_many('test',dic)
    print(rep)

2)范圍查詢

有時(shí)候我們需要范圍比較查詢,比如要查詢 English 成績?yōu)?80~90 ,可以使用比較符:dic = {'English':{'$in':[80,90]}}

  • $lt :小于
  • $lte:小于等于
  • $gt:大于
  • $gte:大于等于
  • $ne:不等于
  • $in:在范圍內(nèi)
  • $nin:不在范圍內(nèi) 

3)計(jì)數(shù)

直接調(diào)用 count() 方法,返回一個(gè) int 類型的數(shù)字

# 計(jì)數(shù)查詢只需要在普通查詢后加上 count() 即可
count = collection.find().count()  
# count = collection.find({'English':{'$gt':90}}).count()

4)排序

排序時(shí),直接調(diào)用sort()方法,并在其中傳入排序的字段及升降序標(biāo)志,返回一個(gè)游標(biāo)對象

# 正序 ASCENDING,倒序 DESCENDING。list()將游標(biāo)對象轉(zhuǎn)成列表
data = list(collection.find(dic).sort('姓名',pymongo.DESCENDING))

7、更新數(shù)據(jù)

首選查到需要更新的數(shù)據(jù),然后將該數(shù)據(jù)更新,返回一個(gè) UpdataResult 對象, raw_result 屬性中包含 update 生效的個(gè)數(shù)。

  • update_one:更新查詢到的第一條數(shù)據(jù)
  • update_many:更新多條數(shù)據(jù)
from pymongo import MongoClient

class mongodb:
    def __init__(self,host,db,port = 27017):
        '''
        :param host: str mongodb地址
        :param db: str 數(shù)據(jù)庫
        :param port: int 端口,默認(rèn)為27017
        '''
        host = host
        db = db
        self.port = port
        client = MongoClient(host=host,port=port)
        self.db = client[db]

    def update_one(self,table,condition,dic):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param condition: dict 查詢條件
        :param dic: dict 更新的數(shù)據(jù)
        :return: 返回UpdateResult對象
        '''
        collection = self.db[table]
        # $set 表示只更新dic字典內(nèi)存在的字段
        rep = collection.update_one(condition,{'$set':dic})
        # 會把之前的數(shù)據(jù)全部用dic字典替換,如果原本存在其他字段,則會被刪除
        # rep = collection.update_one(condition, dic)

        return rep

    def update_many(self,table,condition,dic):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param condition: dict 查詢條件
        :param dic: dict 更新的數(shù)據(jù)
        :return:返回UpdateResult對象
        '''
        collection = self.db[table]
        # $set 表示只更新dic字典內(nèi)存在的字段
        rep = collection.update_many(condition,{'$set':dic})
        # 會把之前的數(shù)據(jù)全部用dic字典替換,如果原本存在其他字段,則會被刪除
        # rep = collection.update_many(condition, dic)

        return rep


if __name__=='__main__':
    condition = {'English':80}
    dic = {'English':60}
    db = mongodb(host='mongodb-monitor.monitor.svc.test.local',db = 'test')
    rep = db.update_one('test',condition,dic)
    print(rep.raw_result)
    # 輸出 {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}

8、刪除

刪除和 update 類似,刪除數(shù)據(jù)后,返回一個(gè) DeleteResult 對象, raw_result 屬性中包含 delete 的個(gè)數(shù)

  • delete_one:刪除查詢到的第一條數(shù)據(jù)
  • delete_many:批量刪除符合查詢條件的數(shù)據(jù)
from pymongo import MongoClient

class mongodb:
    def __init__(self,host,db,port = 27017):
        '''
        :param host: str mongodb地址
        :param db: str 數(shù)據(jù)庫
        :param port: int 端口,默認(rèn)為27017
        '''
        host = host
        db = db
        self.port = port
        client = MongoClient(host=host,port=port)
        self.db = client[db]

    def delete_one(self,table,dic):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param dic: dict 查詢條件
        :return: 返回DeleteResult對象
        '''
        collection = self.db[table]
        rep = collection.delete_one(dic)

        return rep

    def delete_many(self,table,dic):
        '''
        :param table: str 數(shù)據(jù)庫中的集合
        :param dic: dict 查詢條件
        :return: 返回DeleteResult對象
        '''
        collection = self.db[table]
        rep = collection.delete_many(dic)

        return rep


if __name__=='__main__':
    dic = {'English':60}
    db = mongodb(host='localhost',db = 'test')
    rep = db.delete_many('test',dic)
    print(rep.raw_result)
    # 輸出 {'n': 21, 'ok': 1.0}

以上就是如何用python 操作MongoDB數(shù)據(jù)庫的詳細(xì)內(nèi)容,更多關(guān)于python 操作MongoDB數(shù)據(jù)庫的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • MongoDB安裝使用并實(shí)現(xiàn)Python操作數(shù)據(jù)庫
  • 使用python向MongoDB插入時(shí)間字段的操作
  • 用Python實(shí)現(xiàn)定時(shí)備份Mongodb數(shù)據(jù)并上傳到FTP服務(wù)器
  • python連接mongodb數(shù)據(jù)庫操作數(shù)據(jù)示例
  • python爬蟲用mongodb的理由
  • python爬蟲數(shù)據(jù)保存到mongoDB的實(shí)例方法
  • Python MongoDB 插入數(shù)據(jù)時(shí)已存在則不執(zhí)行,不存在則插入的解決方法
  • Python操作Mongodb數(shù)據(jù)庫的方法小結(jié)
  • Python 操作 MongoDB 講解詳細(xì)

標(biāo)簽:江蘇 中山 呼和浩特 湖州 駐馬店 股票 衡水 畢節(jié)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《如何用python 操作MongoDB數(shù)據(jù)庫》,本文關(guān)鍵詞  如,何用,python,操作,MongoDB,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《如何用python 操作MongoDB數(shù)據(jù)庫》相關(guān)的同類信息!
  • 本頁收集關(guān)于如何用python 操作MongoDB數(shù)據(jù)庫的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 微微一笑很倾城干贝微微乳液| 一级A片裸体免费动态图| 女人高潮喷水出白浆是极品吗| 欧美极品第1页专区| 色婷婷粉嫩Av精品综合在线| 日本精品一二三区| 麻麻的屁股夹得我好爽| 印度videos又粗又大| 少妇激情婬乱一区二区三区下载| 捏胸吻胸添奶头gif动态图| 丰满护士| 极品美女扒开粉嫩小泬| 久久99久久99| 中国极品美軳人体GOGO| 在线香蕉精品视频| 视频久久精品| 九九香蕉网| 天堂俺去俺来也www久久婷婷| 成熟美妇在线电影| 公共场合高潮(h)公交车| 年轻的妺妺4伦理HD高清 | 美国z0z0人禽交| 男女激烈试看120秒体验区| 欧美精产国品一二三产品区别在哪| 五月婷婷综合激情网| 天堂禾欧美城网站| 浣肠忍耐极限调教| 亚洲成a×人片在线观看国产| 情侣肉H文| 国语自产免费精品视频一区二区| 成品网站源码隐藏入口| 男女做暧暖xo日韩免费视频| 黑人大猛亚洲女人毛片| 国产农村妇女精品一二区三区| 色婷婷婷丁香亚洲综合不卡| ysl千色t9t9t9| 色呦呦网| 草莓直播下载免费版| 亚洲精选一区二区三区| 女人自扒自慰喷潮A片| 萌白酱粉嫩jk福利在线观看|