MongoDB超全语法大全
MongoDB window安装教程
python模块之 Motor 异步pymongo
一、安装
pip install pymongo
二、连接数据库
import pymongo
connect = pymongo.MongoClient(host='localhost',port=27017)
print('已有的数据库:',connect.list_database_names())
db = connect['new_test'] #如果库不存在,创建库并连接;如果库存在,则直接连接;不创建集合库不会创建成功
collection = db['test'] #如果集合不存在,创建集合并指定集合;如果集合存在,则直接指定集合;不插入数据集合不会创建成功
collection.insert_one({'name':'张三','年龄':20,'性别':'男','职业':'学生'}) #向集合插入数据
print('当前库下的表:',db.list_collection_names())
print('当前集合下的数据:',collection.find_one())
三、基本函数
- pymongo.MongoClient 连接数据库
import pymongo
connect = pymongo.MongoClient(
host, #数据库ip地址
port #数据库端口
)
- connect.list_database_names() 查询所有数据库
- connect.get_database(name) 获取数据库,如果库不存在则创建库,等同于:connect['new_test']
- connect.drop_database(name) 删除数据库
- 创建数据
- collection.insert_one(document) 插入一条数据,document插入的数据:dict
- collection.insert_many(documents) 插入多条数据,documents插入的数据集合: list
- 查询数据
'''创建测试数据'''
import pymongo
connect = pymongo.MongoClient(host='localhost', port=27017)
db = connect['new_test'] # 如果库不存在,创建库并连接;如果库存在,则直接连接;不创建集合库不会创建成功
collection = db['test'] # 如果集合不存在,创建集合并指定集合;如果集合存在,则直接指定集合;不插入数据集合不会创建成功
collection.insert_many([
{'name': '张三', 'age': 20, 'gender': '男', 'job': '学生'},
{'name': '端木语山', 'age': 18, 'gender': '男', 'job': '学生'},
{'name': '虞平乐', 'age': 16, 'gender': '女', 'job': '学生'},
{'name': '焦彭丹', 'age': 21, 'gender': '女', 'job': '学生'},
{'name': '戚晓彤', 'age': 17, 'gender': '未知', 'job': '学生'},
{'name': '仙冰莹', 'age': 28, 'gender': '未知', 'job': '医生'},
{'name': '仰立辉', 'age': 27, 'gender': '未知', 'job': '警察'},
{'name': '卜曾', 'age': 23, 'gender': '女', 'job': '外卖员'},
{'name': '习宏阔', 'age': 22, 'gender': '男', 'job': '学生'},
{'name': '段正祥', 'age': 26, 'gender': '男', 'job': '快递员'},
{'name': '叶痴柏', 'age': 26, 'gender': '未知', 'job': '程序员'},
{'name': '允玥', 'age': 25, 'gender': '女', 'job': '销售'},
{'name': '臧静云', 'age': 24, 'gender': '女', 'job': '学生'},
{'name': '诗兰月', 'age': 21, 'gender': '女', 'job': '学生'},
{'name': '回石', 'age': 30, 'gender': '男', 'job': '程序员'},
{'name': '金成龙', 'age': 12, 'gender': '男', 'job': '学生'},
{'name': '苗舒兰', 'age': 15, 'gender': '女', 'job': '学生'},
{'name': '郎兆', 'age': 19, 'gender': '男', 'job': '学生'}
]) # 向集合插入数据
- collection.find(filter) 查询数据,filter筛选条件:dict,无查询条件查询全部数据
'''查询全部数据'''
find_data = collection.find()
for data in find_data:
print(data)
'''查询性别为男且职业时学生的数据'''
find_data = collection.find({'gender':'男','job':'学生'})
for data in find_data:
print(data)
'''查询年龄小于20的数据:$lt操作符查询'''
find_data = collection.find({'age':{'$lt': 20}})
for data in find_data:
print(data)
'''查询年龄小于20和职业是程序员的数据:$or操作符查询'''
find_data = collection.find({'$or':[{'age':{'$lt':20}},{'job':'程序员'}]})
for data in find_data:
print(data)
'''查询职业为:'程序员','警察','医生'的数据:$in操作符查询'''
find_data = collection.find({'job':{'$in':['程序员','警察','医生']}})
for data in find_data:
print(data)
'''查询职业不为学生的数据:$not操作符查询'''
find_data = collection.find({'job':{'$not':{'$eq':'学生'}}})
for data in find_data:
print(data)
- collection.find(filter).limit(limit) 查询一定数量的数据
find_data = collection.find().limit(2)
for data in find_data:
print(data)
- collection.find(filter).sort(key_or_list,direction) 排序查询
collection.find().sort('age',pymongo.ASCENDING) #升序
collection.find().sort('age',pymongo.DESCENDING) #降序
- collection.find_one(filter) 查询一条数据,filter筛选条件:dict,无查询条件查询第一条数据
- collection.find_one_and_delete(filter) 查询一条数据,并删除
collection.find_one_and_delete({'name':{'$exists':False}}) #删除name不存在的数据
- collection.find_one_and_replace(filter,replacement) 查询一条数据并替换
collection.find_one_and_replace({'age':26},{'age':22})
- collection.find_one_and_update(filter,update) 查询一条数据并修改
collection.find_one_and_update({'age':26},{'$set':{'age':25}})
- collection.aggregate([match, group, sort]) 聚合查询
collection.aggregate([{'$group': {'_id': "$gender",'counter': {'$sum': 1}}}])
- collection.find_raw_batches(filter)
find_data = collection.find_raw_batches({'name':'李四'})
data = next(find_data)
for batch in bson.decode_all(data):
print(batch)
- 修改数据
- collection.update_one(filter,update) 修改一条数据
- collection.update_many(filter,update) 修改多条数据
- collection.replace_one(filter,replacement) 替换一条数据
- 删除数据
- collection.delete_one(filter) 删除一条数据
- collection.delete_many(filter) 删除多条数据