当前的python版本 3.7
声明
当前的内容是基于Python和前面的MongoDB的版本实现的,使用Python中的pymongo模块操作MongoDB数据库,用于记录本人对MongoDB数据库的操作,当前的学习来源:菜鸟教程
1.连接当前的MongoDB数据库
准备的数据,一个db数据库,db数据库中的student集合
# 使用当前的python操作当前的mongodb这个数据库
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
# 输出当前所有的数据库的名称
dblist = myclient.list_database_names()
for i in dblist:
print(i)
# 使用db数据库
mydb = myclient["db"]
db_collections_name = mydb.list_collection_names()
# 输出当前的所有的db中的集合的名称
print("输出当前的db数据中的所有的集合")
for c in db_collections_name:
print(c)
# 获取当前的student这个集合
student_collection = mydb.get_collection("student")
if mydb is not None and myclient is not None:
myclient.close()
结果:
2.实现对当前Document数据的查询
# 使用当前的pymongo操作当前的mongo数据库,实现数据的查询数据操作
import pymongo
try:
with pymongo.MongoClient("mongodb://localhost:27017/") as mongoCli:
db = mongoCli.get_database("db")
student_collection = db.get_collection("student")
# 使用当前的find_obe方法默认查询的就是第一条数据
print("使用当前的find_obe方法默认查询的就是第一条数据")
result = student_collection.find_one()
print(result)
print("=========================================")
# 使用当前的find方法查询所有的数据,这个数据必须使用迭代方式显示
print("使用当前的find方法查询所有的数据")
for record in student_collection.find():
print(record)
print("=========================================")
# 使用当前的特殊的find查询,使用当前的01的结果就是,_id如果标记为0,就表示不显示,如果需要查询的时候就使用1表示,如果后面的出现了0就会出现问题
# 当前的_id后面不能同时出现当前的0和1,操作,不想要显示就可以不写key即可,需要显示就使用key:1
print("使用当前的find方法查询所有的数据,显示特定的数据")
for record in student_collection.find({}, {"_id": 0, "username": 1, "age": 1, "email": 1}):
print(record)
print("=========================================")
# 按照条件进行查询数据
print("按照条件进行查询username为张三的数据")
queryCondition = {"username": "张三"}
for s in student_collection.find(queryCondition):
print(s)
print("=========================================")
# 使用当前的正则方式进行当前的数据的查询的操作(使用正则匹配当前的username中以张开头的数据)
print("按照条件进行查询username以张开头的数据")
for s in student_collection.find({"username": {"$regex": "^张"}}):
print(s)
print("=========================================")
# 条件查询操作,查询当前的数据中年龄大于20的数据
print("按照条件进行查询age大于18的数据")
for s in student_collection.find({"age": {"$gt": 18}}):
print(s)
print("=========================================")
# 返回指定的条数的数据
print("返回指定的条数的数据")
for s in student_collection.find().limit(3):
print(s)
print("=========================================")
except Exception as e:
print(e)
结果:
3.数据的插入操作
# 用当前的mongodb中的数据的插入操作
import pymongo
try:
with pymongo.MongoClient("mongodb://localhost:27017/") as client:
# 默认当前database通过获取的方式创建,如果获取的database名字不存在就创建这个database,否则就使用这个database
db = client.get_database("db1")
# 在指定的数据库中添加一个test集合
db.create_collection("test")
# 获取这个集合
test_collection = db.get_collection("test")
# 向当前的collection集合中插入一条文档记录
col = {"name": "菜鸟教程"}
test_collection.insert_one(col)
# 测试向当前的MongoDB中添加多条数据
cols = [
{"name": "CSDN"},
{"name": "阿里云大学"},
{"name": "网易公开课"}
]
test_collection.insert_many(cols)
# 测试添加的时候获取当前插入数据的_id
col = {"name":"IBM开发者平台"}
result = test_collection.insert_one(col)
print(result.inserted_id)
# 所以默认的当前的MongoDB插入数据的操作的时候会返回当前的_id编号
# 通过上面发现当前的插入数据操作会默认生成_id
# 现在测试主动插入_id编号
col = {"_id": 1, "name": "博客园"}
result = test_collection.insert_one(col)
print(result.inserted_id)
# 执行成功,如果当前的key中出现同名的key就会报错
except Exception as e:
print("连接出现错误:{}".format(e))
结果:
4.数据的更新操作
# 修改数据操作
import pymongo
try:
with pymongo.MongoClient("mongodb://localhost:27017/") as client:
db = client.get_database("db")
student_collection = db.get_collection("student")
print("更新前的结果===================")
for s in student_collection.find():
print(s)
filter = {"username": "张三"}
update = {"$set": {"username": "update张三"}}
# update only works with $ operators,当前的更新操作只能在$上工作
student_collection.update_one(filter, update)
# 使用匹配方式更新,如果年龄大于18,就将密码重置未000000,永远只更新第一个数据
filter = {"age": {"$gt": 18}}
seter = {"$set": {"pwd": "000000"}}
student_collection.update_one(filter, seter)
print("更新后的结果===================")
for s in student_collection.find():
print(s)
except Exception as e:
print("出现错误了:{}".format(e))
结果:
5.数据的删除操作
# 用于测试当前的对MongoDB数据库的删除操作
import pymongo
try:
with pymongo.MongoClient("mongodb://localhost:27017/") as client:
db1 = client.get_database("db1")
test_collection = db1.get_collection("test")
# 删除_id为1的文档
# filter = {"_id": 1}
# result = test_collection.delete_one(filter)
# print(result)
# 删除成功
# 删除name中包含开子的数据
# filter = {"name": {"$regex": ".*开.*"}}
# result = test_collection.delete_many(filter)
# print(result)
# 使用当前的批量删除成功
# 删除当前的test中的所有的文档
# result = test_collection.delete_many({})
# print(result.deleted_count)
# 删除当前的db1集合中的所有的文档
# 删除当前的test集合
result = test_collection.drop()
print(result)
# 发现当前的test如果被删除的话,其中db1中如果没有数据的话db1就会被删除
except Exception as e:
print("出现错误:{}".format(e))
删除数据操作就不显示了
6.查询后排序数据操作
# 用于测试当前的pymongo对当前的mongodb数据的操作,简单的排序操作
import pymongo
try:
with pymongo.MongoClient("mongodb://localhost:27017/") as client:
db1 = client.get_database("db1")
test_collection = db1.get_collection("test")
# 将当前的test集合中的数据按照当前的name进行排序,默认是按照升序排列
for s in test_collection.find().sort("name"):
print(s)
print()
# 按照name进行降序排列
for s in test_collection.find().sort("name", -1):
print(s)
# 默认当前的排序方式未升序1,使用-1就是降序
except Exception as e:
print("当前的操作出现错误:{}".format(e))
结果:
7.总结
1.使用python操作MongoDB数据库的时候需要使用pymongo这个模块
2.可以使用pymongo.MongoClient连接当前的MongoDB数据库,返回client
3.然后通过获取的client可以获取当前的database,通过当前的database获取到collection,通过collection可以获取到Document
4.通过collection对象可以实现对数据的增删改查操作,insert,delete,update,find
以上纯属个人见解,如有问题请联系本人!