学习日志 Python+MongoDB

Python+MongoDB

1. 插入数据

insert_one(doc)#插入一条
insert_many(doc,ordered=True/False)#插入多条
#True 代表顺序插入,若有一条插入失败,则批量插入失败
#False 代表不启用顺序插入,插入失败的数据不硬性其他数据的插入

连接MongoDB服务器

import pymongo
from pymongo import MongoClient#导入连接函数
conn = MongoClient('localhost')#连接
db = conn.myschool#连接myschool这个库
employees = db.stuinfo#创建第一个集合stuinfo
# employees.remove(None)#清空
studentsInfo = db.stuinfo

插入一条记录

zhangsan = {'name':'zhangsan',
            'age':30,
            'sex':'boy',
            'contact':{
                'email1':'[email protected]',
                'email2':'[email protected]'
                        }
            }
employees.insert_one(zhangsan)

2. 插入多条记录

lishi = {'name':'lishi','habit':{'habit1':'eat','habit2':'sleep'}}
wangwu = {'name':'wangwu','age':20,'sex':'boy'}
result = [lishi,wangwu]
employees.insert_many(result,True)
print('插入成功!')

3. 查询数据

cursor = studentsInfo.find({})
for student in cursor:
    print(student)
conn.close()#关闭连接

print('按条件查询')
cursor = studentsInfo.find({'name':'zhangsan'})
for student in cursor:
    print(student)

print('按条件查询(查询条件加操作符)')
cursor = studentsInfo.find({'name':{'$in':['zhangsan','lishi']}})
for student in cursor:
    print(student)

print('按条件查询(age大于25岁)')
cursor = studentsInfo.find({'age':{'$gt':25}})
for student in cursor:
    print(student)

print('条件查询(or 方式查询)')
cursor = studentsInfo.find({'$or':[{'name':{'$in':['zhangsan','wangwu']}},{'age':{'$gt':30}}]})
for student in cursor:
    print(student)

print('条件查询(and 方式查询)')
cursor = studentsInfo.find({'$and':[{'name':{'$in':['zhangsan','lishi']}},{'age':{'$gt':25}}]})
for student in cursor:
    print(student)

print('子条件查询(嵌套模式查询)')
cursor = studentsInfo.find({'contact.email1':'[email protected]'})
for student in cursor:
    print(student)

4. 更新数据

print('$set:更新操作')
studentsInfo.update_one({'name':'wangwu'},{'$set':{'sex':'girl'}})
cursor = studentsInfo.find({})
for student in cursor:
    print(student)

print('$ine:如果记录中没有这个字段,会增加此字段')
studentsInfo.update_many({},{'$inc':{'age':2}})
cursor = studentsInfo.find({})
for student in cursor:
    print(student)

# $min($max) 的意思是和当前值比较,取最小(最大)值来进行更新
studentsInfo.update_many({'name':{'$in':['zhangsan','lishi']}},{'$max':{'age':20}})
cursor = studentsInfo.find({})
for student in cursor:
    print(student)

5. 删除数据

studentsInfo.delete_one({'age':20})#删除单条
studentsInfo.delete_many({'name':'lishi'})#删除包含的所有

6. 查询同时更新

cursor = studentsInfo.find_one_and_update({'name':'wangwu'},{'$set':{'city':'sahnghai'},'$inc':{'age':2}})
print(cursor)

练习

import pymongo
from pymongo import MongoClient
conn = MongoClient('localhost')
db = conn.employee
employee_info = db.employeel
employee_salary = db.employeel

dingsong={'name':'dingsong','salary':238000}
zhangsan={'name':'zhangsan','salary':1500}
lisi={'name':'lisi','salary':300}
wangwu={'name':'wangwu','salary':2500}
result = [dingsong,zhangsan,lisi,wangwu]
employee_info.insert_many(result,True)

cursor = employee_salary.find({'salary':{'$gt':2000}})
for employees in cursor:
    print(employees)

employee_salary.update_many({},{'$inc':{'salary':3000}})
cursor = employee_salary.find({})
for employees in cursor:
    print(employees)

employee_salary.delete_many({'name':'dingsong'})
cursor = employee_salary.find({})
for employees in cursor:
    print(employees)

猜你喜欢

转载自blog.csdn.net/qq_27171347/article/details/81019425