MongoDB数据库是一种开源的跨平台的数据库,主要特点如下:
数据存储没有模式:每个文档的模式可以不同,不仅数据类型可以不同,结构也可以不同具有很强的容易扩展性:文档数据可以被自动分割处理支持高并发书写:集群提高读写性能,甚至可以建立读写分享的集群服务器支持海量存储:内置GridFS,支持大容量的分布式存储。实验效果
实验代码:
from pymongo import MongoClient def output_all(): stu = collection.find() for item in stu: print(item) if __name__ == '__main__': print('建立连接...') client = MongoClient(host='localhost',port=27017) db = client.test collection = db.students students = {'id':'20170101','name':'john','age':20,'gender':'male'} students1 = {'id':'20170102','name':'Amy','age':20,'gender':'male'} students2 = {'id':'20170103','name':'Linda','age':20,'gender':'male'} # 每次运行清空表 print('清空表信息.....') collection.drop() # 插入信息 collection.insert_one(students) collection.insert_many([students1,students2]) stu = collection.find() # 查找全部 output_all() print('查找叫做john的....') # 查询叫做john的 stu = collection.find({'name':'john'}) for item in stu: print(item) print('删除叫做john...') stu = collection.delete_one({'name': 'john'}) stu = collection.find() output_all() # 修改名字将Amy修改为Anna print('修改Amay叫做Anna...') collection.update_one({'name':'Amy'},{'$set':{'name':'Anna'}}) stu = collection.find() output_all()