flask基础 MongoDB
时间:2021-07-01 10:21:17
帮助过:29人阅读
pymongo
# 建立连接,mongo的默认端口是27017
mongo_client = pymongo.MongoClient(host=
‘127.0.0.1‘,port=27017
)
# 连接s12day117数据库
mongo_db = mongo_client[
‘s12day117‘]
# 查询
res =
mongo_db.user.find()
print(res)
for i
in res:
print(i)
res = mongo_db.user.find_one({
‘name‘:
‘yang‘})
print(res)
#插入
res =
mongo_db.chat.inset_one({})
print(res,res.inserted_id)
res =
mongo_db.chat.inset_many([{},{},{}])
print(res,res.inserted_ids)
# 改
res = mongo_db.user.update_one({
‘name‘:
‘yang‘},{
‘$set‘:{
‘age‘:888
}})
res = mongo_db.user.update_many({
"age":{
"$gte":0}},{
"$set":{
"age":888
}})
print(res,dir(res),res.modified_count)
# 删除
res = mongo_db.user.delete_one({
"name":
"xiaozhuer"})
res =
mongo_db.user.delete_many({})
print(res,dir(res),res.deleted_count)
res = mongo_db.user.find_one({
"name":
"yang"})
print(res)
for index,item
in enumerate(res.get(
‘course‘)):
if res.get(
‘course‘)[index][
‘course_name‘] ==
‘linux‘:
res.get(‘course‘)[index][
‘course_name‘] =
‘django框架‘
res = mongo_db.user.update_one({
‘name‘:
‘yang‘},{
‘$set‘:res})
flask基础 MongoDB
标签:大写 ring 根据 当前日期 代码 目录 cli == image