python 包之 mongodb 數據庫操作教程

      網友投稿 884 2025-03-31

      一、安裝

      pip install pymongo

      二、連接數據庫

      import pymongo # 方式一 client = pymongo.MongoClient('MongoDB://localhost:27017') # 方式二 client = pymongo.MongoClient('localhost',27017) # 方式三,有密碼認證 client = pymongo.MongoClient('localhost', 27017, username='xxx', password='xxx')

      python 包之 mongodb 數據庫操作教程

      三、創建數據庫

      import pymongo # 連接 client = pymongo.MongoClient('MongoDB://localhost:27017') # 創建test數據庫 db = client.test # 或者 db = client['test'] print(db)

      四、所有數據庫

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') dbs = client.list_database_names()

      五、創建集合

      也就是數據庫中的表

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創建test數據庫 db = client.test # 創建表 collections = db.user # 或者 collections = db['user'] # 刪除表 collections.drop()

      六、插入數據

      insert_one:插入一條數據

      insert_many:插入多條數據

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創建test數據庫 db = client.test # 創建表 collections = db.user # 創建文檔數據 user1 = { 'name': 'autofelix', 'age': '25', 'height': '172', 'weight': '60' } user2 = { 'name': '飛兔小哥', 'age': '28', 'height': '182', 'weight': '70' } # 插入一條文檔集合 result = collections.insert_one(user1) print(result) print(result.inserted_id) # 插入多條文檔集合 result = collections.insert_many([user1, user2]) print(result) print(result.inserted_ids)

      七、查詢數據

      find:查詢多條數據

      find_one:查詢一條數據

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創建test數據庫 db = client.test # 創建表 collections = db.user # 查詢所有 collections.find() # 查詢最近一條 collections.find_one() # 根據條件查詢 collections.find_one({'age':25})

      八、高級查詢

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創建test數據庫 db = client.test # 創建表 collections = db.user # 跳過第一條查到的數據 collections.find({'age':{'$gt':10}},['height','age']).skip(1) # limit限制查詢條數 collections.find({'age':{'$gt':10}},['height','age']).limit(1) # 多條件查詢 collections.find_one({'height':{'$gt':150},'age':{'$lt':26,'$gt':10}}) # in查詢,查詢年齡在25,26,32的數據 collections.find({'age':{'$in':[25, 26, 32]}}) # or查詢,查詢年齡小于等于23或者大于等于29的數據 collections.find({'$or':[{'age':{'$lte':23}}, {'age':{'$gte':29}}]}) # exists查詢 collections.find({'age':{'$exists':True}}) # 正則查詢 collections.find({'name':{'$regex':r'.*auto.*'}})

      九、count統計

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創建test數據庫 db = client.test # 創建表 collections = db.user # 統計集合中總共有多少條數據 collections.find().count() # 統計集合中年齡大于10歲的共有多少條數據 collections.find({'age':{'$gt':10}}).count()

      十、修改數據

      update_one:修改一條數據

      update_many:修改多條數據

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創建test數據庫 db = client.test # 創建表 collections = db.user # 修改一條數據 collections.update_one({'name': 'autofelix'}, {'$set': {'name': '大神'}}) # 修改多條數據 collections.update_many({'name': 'autofelix'}, {'$set': {'name': '大神'}})

      十一、刪除數據

      delete_one:刪除一條數據

      delete_many:刪除多條數據

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創建test數據庫 db = client.test # 創建表 collections = db.user # 刪除一條數據 collections.delete_one({'name': 'autofelix'}) # 刪除多條數據 collections.delete_many({'name': 'autofelix'}) # 刪除所有數據 collections.delete_many({})

      十二、數據排序

      import pymongo client = pymongo.MongoClient('mongodb://localhost:27017') # 創建test數據庫 db = client.test # 創建表 collections = db.user # 對字段 age 按升序排序 collections.find().sort('age') # 對字段 age 按降序排序 collections.find().sort('age', -1) # 多字段排序 collections.find().sort((('age',pymongo.ASCENDING),('height',pymongo.ASCENDING)))

      MongoDB Python 數據庫

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:office2010激活程序
      下一篇:諸城外貿安全生產管理平臺(諸城市外貿)
      相關文章
      亚洲人成77777在线播放网站不卡| 亚洲精品制服丝袜四区| 久久久久久A亚洲欧洲AV冫| 亚洲欧美一区二区三区日产| 亚洲男人的天堂在线| 伊人久久综在合线亚洲2019| 亚洲AV区无码字幕中文色| 亚洲精品无码MV在线观看| 国产AV无码专区亚洲AV手机麻豆| 亚洲最大av无码网址| 精品国产亚洲男女在线线电影 | 久久精品国产亚洲av高清漫画| 亚洲色欲色欲www在线丝 | 亚洲精品国产情侣av在线| 亚洲AV无码AV男人的天堂| 亚洲av永久无码精品漫画 | 亚洲成A人片在线观看WWW| 久久精品亚洲一区二区| 亚洲韩国—中文字幕| 91精品国产亚洲爽啪在线观看| 亚洲精品美女久久久久| 亚洲成人黄色在线观看| 亚洲av永久综合在线观看尤物| 亚洲激情视频图片| 亚洲人成自拍网站在线观看| 欧美日韩亚洲精品| 亚洲国产精品专区在线观看| 亚洲情a成黄在线观看| 亚洲中文字幕久久精品无码APP| 亚洲女同成av人片在线观看| 亚洲AV无码成人精品区蜜桃| 麻豆亚洲AV永久无码精品久久| 337p日本欧洲亚洲大胆色噜噜| 亚洲国产成人精品电影| 亚洲人成综合网站7777香蕉| 亚洲成a人片在线观看天堂无码 | 亚洲AV无码成人精品区蜜桃| 亚洲美女视频一区| 成人区精品一区二区不卡亚洲| 亚洲hairy多毛pics大全| 亚洲成AⅤ人影院在线观看|