2018年7月6日笔记
下文中的操作都是使用python操作mongoDB,所以前提是必须安装python和mongoDB。
在cmd或者PowerShell中运行命令:pip install pymongo
引入pymongo库中的MongoClient类,使用该类的初始化方法实例化一个对象赋值给conn
conn.myschool为数据库school,将其赋值给变量db
db.students为数据库school中的students集合,将其赋值给变量students
定义一个数据类型为字典dict的变量zhangsan
往数据库中的students集合中插入zhangsan的信息文档,students.insert_one(zhangsan)
from pymongo import MongoClient if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.myschool students = db.students zhangsan = dict( name="zhangsan", age=20, sex="boy", cOntact=dict( email1="abc@qq.com", email2="123@qq.com" ) ) students.insert_one(zhangsan)
from pymongo import MongoClient if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.myschool students = db.students lisi = dict( name = 'lisi', habit = dict( habit1 = 'eat', habit2 = 'sleep' ) ) wangwu = dict( name = 'wangwu', age = 20, sex = 'boy' ) students.insert_many([lisi,wangwu])
from pymongo import MongoClient from random import randint if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.school hobby_list = ['sing','drawing','run','football','basketball'] grade_list = ['grade_1_1','grade_1_2','grade_1_3'] for grade in grade_list: collection = db.get_collection(grade) collection.drop() for i in range(10): student = dict( name = "zhangsan" + str(i), age = randint(1,9), sex = "男" if randint(0,1) else "女", hobby = hobby_list[:randint(0,5)] ) collection.insert_one(student)
在进行查询的时候,必须要先执行2-3节中的代码,确保school数据库中插入3个班集合
示例代码如下:
from pymongo import MongoClient if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.school students = db.grade_1_3 cursor = students.find() for i in cursor: print(i)
查询之前先定义一个数据类型为字典dict的变量filter
filter示例,查询年龄等于4的文档:filter = {"age":4}
代码示例如下,代码能够成功运行,具体的数据库名和集合名要根据自己电脑中的MongoDB情况进行更改:
from pymongo import MongoClient if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.school students = db.grade_1_3 filter = {"age": 4} cursor = students.find(filter) for i in cursor: print(i)
filter中操作符及其含义
from pymongo import MongoClient if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.school students = db.grade_1_3 filter = {"name": {'$in':['zhangsan2','zhangsan3']}} cursor = students.find(filter) for i in cursor: print(i)
上面一段代码的运行结果如下:
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cda'), 'name': 'zhangsan2', 'age': 3, 'sex': '女', 'hobby': ['sing', 'drawing']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cdb'), 'name': 'zhangsan3', 'age': 3, 'sex': '男', 'hobby': ['sing', 'drawing', 'run', 'football']}
from pymongo import MongoClient if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.school students = db.grade_1_3 filter = {"age": {'$gt':4}} cursor = students.find(filter) for i in cursor: print(i)
上面一段代码的运行结果如下:
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cd8'), 'name': 'zhangsan0', 'age': 7, 'sex': '女', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cd9'), 'name': 'zhangsan1', 'age': 5, 'sex': '女', 'hobby': ['sing', 'drawing', 'run', 'football']}
from pymongo import MongoClient if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.school students = db.grade_1_3 filter = {"age": {'$gte':4}} cursor = students.find(filter) for i in cursor: print(i)
上面一段代码运行结果如下:
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cd8'), 'name': 'zhangsan0', 'age': 7, 'sex': '女', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cd9'), 'name': 'zhangsan1', 'age': 5, 'sex': '女', 'hobby': ['sing', 'drawing', 'run', 'football']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cdc'), 'name': 'zhangsan4', 'age': 4, 'sex': '女', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cdf'), 'name': 'zhangsan7', 'age': 4, 'sex': '女', 'hobby': ['sing', 'drawing']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1ce1'), 'name': 'zhangsan9', 'age': 4, 'sex': '女', 'hobby': ['sing', 'drawing', 'run']}
让所有文档中的sex字段都被修改为“男”。
from pymongo import MongoClient if __name__ == '__main__': cOnn= MongoClient('localhost') db = conn.school students = db.grade_1_3 filter = {"sex":"女"} updater = {"$set":{"sex":"男"}} students.update_many(filter,updater) cursor = students.find() for i in cursor: print(i)
上面一段代码的运行结果如下:
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cd8'), 'name': 'zhangsan0', 'age': 7, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cd9'), 'name': 'zhangsan1', 'age': 5, 'sex': '男', 'hobby': ['sing', 'drawing', 'run', 'football']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cda'), 'name': 'zhangsan2', 'age': 3, 'sex': '男', 'hobby': ['sing', 'drawing']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cdb'), 'name': 'zhangsan3', 'age': 3, 'sex': '男', 'hobby': ['sing', 'drawing', 'run', 'football']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cdc'), 'name': 'zhangsan4', 'age': 4, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cdd'), 'name': 'zhangsan5', 'age': 1, 'sex': '男', 'hobby': ['sing', 'drawing', 'run', 'football']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cde'), 'name': 'zhangsan6', 'age': 2, 'sex': '男', 'hobby': ['sing']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1cdf'), 'name': 'zhangsan7', 'age': 4, 'sex': '男', 'hobby': ['sing', 'drawing']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1ce0'), 'name': 'zhangsan8', 'age': 1, 'sex': '男', 'hobby': ['sing', 'drawing']}
{'_id': ObjectId('5b3f0ea2ba50f64850ef1ce1'), 'name': 'zhangsan9', 'age': 4, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
让所有文档的age字段加2,即让集合grade_1_1中的所有人年龄加2
from pymongo import MongoClient def printMongo(collection,filter={},num=3): cursor = collection.find(filter) for i in cursor[:num]: print(i) if __name__ == "__main__": cOnn= MongoClient("localhost") db = conn.school students = db.grade_1_1 printMongo(students) updater = {"$inc":{"age":2}} students.update_many({},updater) print("MongoDB更新数据后:") printMongo(students)
上面一段代码的运行结果如下:
{'_id': ObjectId('5b403bdc33514c04b891a363'), 'name': 'zhangsan0', 'age': 7, 'sex': '男', 'hobby': ['sing', 'drawing', 'run', 'football', 'basketball']}
{'_id': ObjectId('5b403bdc33514c04b891a364'), 'name': 'zhangsan1', 'age': 4, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b403bdc33514c04b891a365'), 'name': 'zhangsan2', 'age': 7, 'sex': '女', 'hobby': []}
MongoDB更新数据后:
{'_id': ObjectId('5b403bdc33514c04b891a363'), 'name': 'zhangsan0', 'age': 9, 'sex': '男', 'hobby': ['sing', 'drawing', 'run', 'football', 'basketball']}
{'_id': ObjectId('5b403bdc33514c04b891a364'), 'name': 'zhangsan1', 'age': 6, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b403bdc33514c04b891a365'), 'name': 'zhangsan2', 'age': 9, 'sex': '女', 'hobby': []}
与当前值进行比较,更新值为两者中的较小值
下面一段代码将所有age字段大于6的文档中的age字段修改为6
from pymongo import MongoClient def printMongo(collection,filter={},num=5): cursor = collection.find(filter) for i in cursor[:num]: print(i) if __name__ == "__main__": cOnn= MongoClient("localhost") db = conn.school students = db.grade_1_1 printMongo(students) updater = {"$min":{"age":6}} students.update_many({},updater) print("MongoDB更新数据后:") printMongo(students)
上面一段代码的运行结果如下:
{'_id': ObjectId('5b403efd33514c189890748f'), 'name': 'zhangsan0', 'age': 4, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b403efe33514c1898907490'), 'name': 'zhangsan1', 'age': 9, 'sex': '男', 'hobby': []}
{'_id': ObjectId('5b403efe33514c1898907491'), 'name': 'zhangsan2', 'age': 1, 'sex': '男', 'hobby': ['sing']}
{'_id': ObjectId('5b403efe33514c1898907492'), 'name': 'zhangsan3', 'age': 7, 'sex': '女', 'hobby': ['sing', 'drawing', 'run', 'football']}
{'_id': ObjectId('5b403efe33514c1898907493'), 'name': 'zhangsan4', 'age': 2, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
MongoDB更新数据后:
{'_id': ObjectId('5b403efd33514c189890748f'), 'name': 'zhangsan0', 'age': 4, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
{'_id': ObjectId('5b403efe33514c1898907490'), 'name': 'zhangsan1', 'age': 6, 'sex': '男', 'hobby': []}
{'_id': ObjectId('5b403efe33514c1898907491'), 'name': 'zhangsan2', 'age': 1, 'sex': '男', 'hobby': ['sing']}
{'_id': ObjectId('5b403efe33514c1898907492'), 'name': 'zhangsan3', 'age': 6, 'sex': '女', 'hobby': ['sing', 'drawing', 'run', 'football']}
{'_id': ObjectId('5b403efe33514c1898907493'), 'name': 'zhangsan4', 'age': 2, 'sex': '男', 'hobby': ['sing', 'drawing', 'run']}
添加的时间是按照0时区时间计时:比如本文作者实验时间为2018年7月7日12时28分(北京时间东8区),下面一段代码执行时记录的时间是2018年7月7日4时28分。
from pymongo import MongoClient def printMongo(collection,filter={},num=2): cursor = collection.find(filter) for i in cursor[:num]: print(i) if __name__ == "__main__": cOnn= MongoClient("localhost") db = conn.school students = db.grade_1_1 printMongo(students) updater = {"$currentDate":{"create_time":True}} students.update_many({},updater) print("MongoDB更新数据后:") printMongo(students)
上面一段代码的运行结果如下:
{'_id': ObjectId('5b40413a33514c36d8a854ee'), 'name': 'zhangsan0', 'age': 8, 'sex': '女', 'hobby': ['sing', 'drawing']}
{'_id': ObjectId('5b40413a33514c36d8a854ef'), 'name': 'zhangsan1', 'age': 5, 'sex': '男', 'hobby': ['sing', 'drawing', 'run', 'football']}
MongoDB更新数据后:
{'_id': ObjectId('5b40413a33514c36d8a854ee'), 'name': 'zhangsan0', 'age': 8, 'sex': '女', 'hobby': ['sing', 'drawing'], 'create_time': datetime.datetime(2018, 7, 7, 4, 28, 0, 527000)}
{'_id': ObjectId('5b40413a33514c36d8a854ef'), 'name': 'zhangsan1', 'age': 5, 'sex': '男', 'hobby': ['sing', 'drawing', 'run', 'football'], 'create_time': datetime.datetime(2018, 7, 7, 4, 28, 0, 527000)}
delete_many()函数用于删除多条数据
delete_one()函数用于删除一条数据
三个可以利用来操作满足filter筛选条件的第一条文档的函数:
find_one_and_replace()
find_one_and_delete()
find_one_and_update()
新建mongoDB数据库employee,里面有两个集合employee_info、employee_salary
(1)分别添加2个集合的数据
(2)查询employee_salary工资大于2000的;查询employee_info员工名称=xxx)
(3)修改employee_salary集合(将员工工资增加3000)
(4)删除员工aa的信息
4道小题合在一起的代码如下:
from pymongo import MongoClient def printMongo(collection,filter={},num=2): cursor = collection.find(filter) for i in cursor[:num]: print(i) if __name__ == "__main__": cOnn= MongoClient("localhost") db = conn.employee info = db.employee_info salary = db.employee_salary #第1小题 info.drop() salary.drop() info.insert_one({"name":"aa","sex":"男","age":23}) info.insert_one({"name":"bb","sex":"女","age":20}) salary.insert_one({"name":"aa","salary":3000}) salary.insert_one({"name": "bb", "salary": 1800}) printMongo(info) printMongo(salary) # 第2小题 filter = {"salary":{"$gt":2000}} print("找出工资大于2000的员工:") printMongo(salary,filter) filter2 = {'name':"bb"} print("找出姓名为bb的员工") printMongo(info,filter2) ##第3小题 print("没有增加工资时:") printMongo(salary) updater = {"$inc":{"salary":3000}} salary.update_many({},updater) print("增加工资后:") printMongo(salary) ##第4小题 print("没有删除员工aa时打印所有员工信息:") printMongo(info) printMongo(salary) print("删除员工aa后打印所有员工信息") info.delete_one({"name":"aa"}) salary.delete_one({"name":"aa"}) printMongo(info) printMongo(salary)
上面一段代码的运行结果如下:
{'_id': ObjectId('5b404b3333514c1dd0f6c096'), 'name': 'aa', 'sex': '男', 'age': 23}
{'_id': ObjectId('5b404b3333514c1dd0f6c097'), 'name': 'bb', 'sex': '女', 'age': 20}
{'_id': ObjectId('5b404b3333514c1dd0f6c098'), 'name': 'aa', 'salary': 3000}
{'_id': ObjectId('5b404b3333514c1dd0f6c099'), 'name': 'bb', 'salary': 1800}
找出工资大于2000的员工:
{'_id': ObjectId('5b404b3333514c1dd0f6c098'), 'name': 'aa', 'salary': 3000}
找出姓名为bb的员工
{'_id': ObjectId('5b404b3333514c1dd0f6c097'), 'name': 'bb', 'sex': '女', 'age': 20}
没有增加工资时:
{'_id': ObjectId('5b404b3333514c1dd0f6c098'), 'name': 'aa', 'salary': 3000}
{'_id': ObjectId('5b404b3333514c1dd0f6c099'), 'name': 'bb', 'salary': 1800}
增加工资后:
{'_id': ObjectId('5b404b3333514c1dd0f6c098'), 'name': 'aa', 'salary': 6000}
{'_id': ObjectId('5b404b3333514c1dd0f6c099'), 'name': 'bb', 'salary': 4800}
没有删除员工aa时打印所有员工信息:
{'_id': ObjectId('5b404b3333514c1dd0f6c096'), 'name': 'aa', 'sex': '男', 'age': 23}
{'_id': ObjectId('5b404b3333514c1dd0f6c097'), 'name': 'bb', 'sex': '女', 'age': 20}
{'_id': ObjectId('5b404b3333514c1dd0f6c098'), 'name': 'aa', 'salary': 6000}
{'_id': ObjectId('5b404b3333514c1dd0f6c099'), 'name': 'bb', 'salary': 4800}
删除员工aa后打印所有员工信息
{'_id': ObjectId('5b404b3333514c1dd0f6c097'), 'name': 'bb', 'sex': '女', 'age': 20}
{'_id': ObjectId('5b404b3333514c1dd0f6c099'), 'name': 'bb', 'salary': 4800}