python3 操作mongodb数据库

python3 操作mongodb数据库

安装

pip3 install pymongo

本教程连接的是linus系统下安装的mongodb。

python3 操作数据库 增删改查


from pymongo import MongoClient

# 连接mongdb数据库
client = MongoClient('mongodb://192.168.0.107:27017')

# 获取数据库db对象 库的名称 py3
db = client.py3

# 获取集合对象 表的名称 collection ==> mdata
collection = db.mdata

# 利用集合对象操作数据库 增删改查

# 插入数据
# s1 = collection.insert({'type':'123','title':'大的点点滴滴'})

# 查询数据 返回来的是个集合对象 需要for in 遍历
# msg = collection.find({})
# for i in msg:
#     print(i)

# 更新数据
# collection.update({'title':'hfhdjdfhjfd'},{'$set':{'type':'1111111'}})

# 删除数据 都删除满足条件的
# res = collection.remove({'type':'123'})
# print(res)

猜你喜欢

转载自blog.csdn.net/adley_app/article/details/80715630
今日推荐