python 包之 mongodb 資料庫操作教程
阿新 • • 發佈:2022-04-12
一、安裝
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')
三、建立資料庫
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)))