基于pymongo的MongoDB系列操作
from pymongo import MongoClient
import pymongo
import datetime
## 连接本地 MongoDB
client = MongoClient("mongodb://localhost:27017/") #本地库参数可省缺
## 数据库名
client.list_database_names()
['admin', 'config', 'local', 'study']
## 集合(表)名
client.study.list_collection_names()
['mongo_study_sites']
零、MongoDB结构
MongoDB
database_1 数据库1
……
database_n 数据库ncollection_1 表1
……
collection_2 表ndocument_1 行数据1
……
document_n 行数据n
一、增
## 1、新增数据库、集合(插入文档即创建,是无法创建空库空集合的)
docs = {'num': 1,
'desc': '菜鸟教程:MongoDB教程',
'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
client.study.mongo_study_sites.insert_one(docs) # 新增一条文档
for i in client.study.mongo_study_sites.find():
print(i)
{'_id': ObjectId('61d6ea09082b9beb7bca8b08'), 'num': 1, 'desc': '菜鸟教程:MongoDB教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
## 2、新增文档:同时插入多条文档 Bulk Inserts
docs_list = [{'num': 1,
'desc': 'MongoDB菜鸟教程',
'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'},
{'num': 2,
'desc': 'PyMongo官方教程',
'url': 'https://pymongo.readthedocs.io/en/stable/index.html',
'date': datetime.datetime(2022, 1, 5, 23, 8)},
{'num': 3,
'desc': 'PyMongo菜鸟教程',
'url': 'https://www.runoob.com/python3/python-mongodb.html'}
]
client.study.mongo_study_sites.insert_many(docs_list)
for i in client.study.mongo_study_sites.find():
print(i)
{'_id': ObjectId('61d6ea09082b9beb7bca8b08'), 'num': 1, 'desc': '菜鸟教程:MongoDB教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'_id': ObjectId('61d6ea09082b9beb7bca8b09'), 'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'_id': ObjectId('61d6ea09082b9beb7bca8b0a'), 'num': 2, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'date': datetime.datetime(2022, 1, 5, 23, 8)}
{'_id': ObjectId('61d6ea09082b9beb7bca8b0b'), 'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
二、删
## 1、删除文档:一定要制定条件,即使删除全部数据也要输入参数 {}
# 删除一条文档
flter = {'desc': '菜鸟教程:MongoDB教程'}
client.study.mongo_study_sites.delete_one(flter)
# 同时删除所有符合条件文档
client.study.mongo_study_sites.delete_many(flter)
for i in client.study.mongo_study_sites.find():
print(i)
{'_id': ObjectId('61d6ea09082b9beb7bca8b09'), 'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'_id': ObjectId('61d6ea09082b9beb7bca8b0a'), 'num': 2, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'date': datetime.datetime(2022, 1, 5, 23, 8)}
{'_id': ObjectId('61d6ea09082b9beb7bca8b0b'), 'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
## 2、删除集合
client['study']['mongo_study_sites'].drop()
client.study.list_collection_names()
[]
三、改
## 1、修改文档
# 修改一条文档
flter = {'desc': 'PyMongo官方教程'}
upd = {'$set': {'num': 100}}
client['study']['mongo_study_sites'].update_one(flter, upd)
# 修改多条文档
client['study']['mongo_study_sites'].update_many(flter, upd)
for i in client['study']['mongo_study_sites'].find({}, {'_id':0}):
print(i)
print('= ' * 50)
# 注意:如果修改字段不存在,则新增字段
flter = {'desc': 'PyMongo官方教程'}
upd = {'$set': {'score': '8分'}} #'score'不存在
client['study']['mongo_study_sites'].update_one(flter, upd)
for i in client['study']['mongo_study_sites'].find({}, {'_id':0}):
print(i)
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'num': 100, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'date': datetime.datetime(2022, 1, 5, 23, 8)}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'num': 100, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'date': datetime.datetime(2022, 1, 5, 23, 8), 'score': '8分'}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
## 2、删除字段
flter = {'desc': 'PyMongo官方教程'}
upd = {'$unset': {'score': '8分'}} #删除score字段
client['study']['mongo_study_sites'].update_one(flter, upd)
for i in client['study']['mongo_study_sites'].find({}, {'_id':0}):
print(i)
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'num': 100, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'date': datetime.datetime(2022, 1, 5, 23, 8)}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
## 3、修改字段名称
flter = {'desc': 'PyMongo官方教程'}
upd = {'$rename': {'date': 'DT'}} #重命名date为DT
client['study']['mongo_study_sites'].update_one(flter, upd)
for i in client['study']['mongo_study_sites'].find({}, {'_id':0}):
print(i)
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'num': 100, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'DT': datetime.datetime(2022, 1, 5, 23, 8)}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
四、查
## 1、查询全部find()、查询一条find_one()
flter = {'num': 3}
for i in client.study.mongo_study_sites.find(flter, {'_id':0, 'desc':1, 'url':1}): # 指定查询字段
print(i)
print('= ' * 50)
for i in client['study']['mongo_study_sites'].find_one(flter):
print(i)
{'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
_id
num
desc
url
## 2、查询存在某个字段的文档
flter = {'DT': {'$exists': True}}
for i in client.study.mongo_study_sites.find(flter, {'_id':0}): # 指定查询字段
print(i)
{'num': 100, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'DT': datetime.datetime(2022, 1, 5, 23, 8)}
## 3、比较查询
# $eq 等于
# $ne 不等于
# $gt 大于
# $gte 大于等于
# $lt 小于
# $lte 小于等于
# $in 存在于
# $nin 不存在于
flter = {'num': {'$gte': 2}}
for i in client.study.mongo_study_sites.find(flter, {'_id':0}): # 指定查询字段
print(i)
print('= ' * 50)
flter = {'num': {'$in': [1,3]}}
for i in client.study.mongo_study_sites.find(flter, {'_id':0}): # 指定查询字段
print(i)
{'num': 100, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'DT': datetime.datetime(2022, 1, 5, 23, 8)}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
## 4、逻辑查询
# $and 且
# $or 或
# $not 非
# $nor
flter = {'$or':[{'desc': 'PyMongo菜鸟教程'}, {'num': 1}]}
for i in client.study.mongo_study_sites.find(flter, {'_id':0}): # 指定查询字段
print(i)
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
## 5、正则表达式
flter = {'desc': {'$regex': r'菜鸟教程$'}} #desc以“菜鸟教程”结尾
for i in client.study.mongo_study_sites.find(flter, {'_id':0}): # 指定查询字段
print(i)
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
## 6、指定返回文档数
flter = {'desc': {'$regex': r'菜鸟教程$'}} #desc以“菜鸟教程”结尾
for i in client.study.mongo_study_sites.find(flter, {'_id':0}).limit(1): # 指定查询字段
print(i)
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
## 7、排序
for i in client.study.mongo_study_sites.find({}, {'_id':0}).sort('num',-1): # -1倒序,1(或省缺)正序
print(i)
{'num': 100, 'desc': 'PyMongo官方教程', 'url': 'https://pymongo.readthedocs.io/en/stable/index.html', 'DT': datetime.datetime(2022, 1, 5, 23, 8)}
{'num': 3, 'desc': 'PyMongo菜鸟教程', 'url': 'https://www.runoob.com/python3/python-mongodb.html'}
{'num': 1, 'desc': 'MongoDB菜鸟教程', 'url': 'https://www.runoob.com/mongodb/mongodb-tutorial.html'}
五、聚合操作
## 0、创建数据集
docs_list = [{'name':'张三', 'age': 26, 'sex':'男', 'company':'A公司', 'income':6000},
{'name':'李四', 'age': 30, 'sex':'男', 'company':'B公司', 'income':5000},
{'name':'小红', 'age': 25, 'sex':'女', 'company':'A公司', 'income':5500},
{'name':'王二', 'age': 26, 'sex':'男', 'company':'A公司', 'income':9000},
{'name':'小芳', 'age': 26, 'sex':'女', 'company':'B公司', 'income':7000}]
client['study']['employee'].insert_many(docs_list)
for i in client['study']['employee'].find({}):
print(i)
{'_id': ObjectId('61d70149082b9beb7bca8b1b'), 'name': '张三', 'age': 26, 'sex': '男', 'company': 'A公司', 'income': 6000}
{'_id': ObjectId('61d70149082b9beb7bca8b1c'), 'name': '李四', 'age': 30, 'sex': '男', 'company': 'B公司', 'income': 5000}
{'_id': ObjectId('61d70149082b9beb7bca8b1d'), 'name': '小红', 'age': 25, 'sex': '女', 'company': 'A公司', 'income': 5500}
{'_id': ObjectId('61d70149082b9beb7bca8b1e'), 'name': '王二', 'age': 26, 'sex': '男', 'company': 'A公司', 'income': 9000}
{'_id': ObjectId('61d70149082b9beb7bca8b1f'), 'name': '小芳', 'age': 26, 'sex': '女', 'company': 'B公司', 'income': 7000}
## 1、分组计数 '$sum':1
grp = {'$group': {'_id':'$sex', 'cnt':{'$sum':1}}}
for i in client['study']['employee'].aggregate([grp]): #注意参数为列表
print(i)
{'_id': '男', 'cnt': 3}
{'_id': '女', 'cnt': 2}
## 2、分组最值 $max、$min
grp = {'$group': {'_id':'$sex', 'max_income':{'$max':'$income'}, 'min_income':{'$min':'$income'}}} #男女均龄
for i in client['study']['employee'].aggregate([grp]): #注意参数为列表
print(i)
{'_id': '男', 'max_income': 9000, 'min_income': 5000}
{'_id': '女', 'max_income': 7000, 'min_income': 5500}
## 3、分组平均$avg、求和$sum
grp = {'$group': {'_id':'$sex', 'sum_income':{'$sum':'$income'}, 'avg_income':{'$avg':'$income'}}}
for i in client['study']['employee'].aggregate([grp]): #注意参数为列表
print(i)
{'_id': '男', 'sum_income': 20000, 'avg_income': 6666.666666666667}
{'_id': '女', 'sum_income': 12500, 'avg_income': 6250.0}
for i in client['study']['employee'].find({}).sort('income', 1):
print(i)
{'_id': ObjectId('61d70149082b9beb7bca8b1c'), 'name': '李四', 'age': 30, 'sex': '男', 'company': 'B公司', 'income': 5000}
{'_id': ObjectId('61d70149082b9beb7bca8b1d'), 'name': '小红', 'age': 25, 'sex': '女', 'company': 'A公司', 'income': 5500}
{'_id': ObjectId('61d70149082b9beb7bca8b1b'), 'name': '张三', 'age': 26, 'sex': '男', 'company': 'A公司', 'income': 6000}
{'_id': ObjectId('61d70149082b9beb7bca8b1f'), 'name': '小芳', 'age': 26, 'sex': '女', 'company': 'B公司', 'income': 7000}
{'_id': ObjectId('61d70149082b9beb7bca8b1e'), 'name': '王二', 'age': 26, 'sex': '男', 'company': 'A公司', 'income': 9000}
## 4、最前$first、最后$last:分组排序取其最,类似窗口函数
srt = {'$sort':{'income':1}}
grp = {'$group': {'_id':'$sex', 'first_income':{'$first':'$income'}, 'last_income':{'$last':'$income'}}}
for i in client['study']['employee'].aggregate([srt,grp]): #注意参数为列表
print(i)
{'_id': '男', 'first_income': 5000, 'last_income': 9000}
{'_id': '女', 'first_income': 5500, 'last_income': 7000}
## 5、条件+分组
mach = {'$match': {'age':{'$gt':25}}} #年龄>25
grp = {'$group': {'_id':'$sex', 'cnt':{'$sum':1}}}
for i in client['study']['employee'].aggregate([mach, grp]): #注意参数为列表
print(i)
{'_id': '男', 'cnt': 3}
{'_id': '女', 'cnt': 1}
## 6、多字段分组
grps = {'$group': {'_id':{'公司':'$company','性别':'$sex'}, '最高工资':{'$max':'$income'}}}
for i in client['study']['employee'].aggregate([grps]): #注意参数为列表
print(i)
{'_id': {'公司': 'A公司', '性别': '男'}, '最高工资': 9000}
{'_id': {'公司': 'B公司', '性别': '女'}, '最高工资': 7000}
{'_id': {'公司': 'A公司', '性别': '女'}, '最高工资': 5500}
{'_id': {'公司': 'B公司', '性别': '男'}, '最高工资': 5000}
## 7、分组汇集列表
#去重 $addToSet
grp1 = {'$group': {'_id':'$company', 'list_sex':{'$addToSet':'$sex'}}}
for i in client['study']['employee'].aggregate([grp1]): #注意参数为列表
print(i)
print('= ' * 50)
#不去重 $push
grp2 = {'$group': {'_id':'$company', 'list_sex':{'$push':'$sex'}}}
for i in client['study']['employee'].aggregate([grp2]): #注意参数为列表
print(i)
{'_id': 'B公司', 'list_sex': ['男', '女']}
{'_id': 'A公司', 'list_sex': ['男', '女']}
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
{'_id': 'B公司', 'list_sex': ['男', '女']}
{'_id': 'A公司', 'list_sex': ['男', '女', '男']}
## 仅查看文档数可用 count_documents()
client['study']['employee'].count_documents({})
5
六、索引
## 创建索引
client['study']['mongo_study_sites'].create_index([('desc', pymongo.ASCENDING)], unique=True) # 唯一索引,可防止重复数据
# 查看索引信息
print(list(client['study']['mongo_study_sites'].index_information())) #'_id'是默认的索引
# 尝试插入一条重复数据
doc = {'desc': 'MongoDB菜鸟教程'}
try:
client['study']['mongo_study_sites'].insert_one(doc)
except Exception as e: #查看错误
print(str(e))
['_id_', 'desc_1']
E11000 duplicate key error collection: study.mongo_study_sites index: desc_1 dup key: { desc: "MongoDB菜鸟教程" }, full error: {'index': 0, 'code': 11000, 'keyPattern': {'desc': 1}, 'keyValue': {'desc': 'MongoDB菜鸟教程'}, 'errmsg': 'E11000 duplicate key error collection: study.mongo_study_sites index: desc_1 dup key: { desc: "MongoDB菜鸟教程" }'}
posted on 2022-09-18 15:31 chenxiaoyuan 阅读(132) 评论(0) 编辑 收藏 举报