使用pycharm进行数据库的操作更简单也更便捷
#下载的两种方式,第二种更快 pip install pymongo pip isntall pymongo -i https://pypi.douban.com/simple
在pycharm中进行mongoDB数据库的编写
import pymongo#调用模块
# 1. 建立连接
link = pymongo.MongoClient() # 实例化一个link对象
# 2. 指定数据库
db = link['python']
# 3. 指定集合
my_col = db['student']
# 4.操作文档(增删改查)
# 增 insert_one insert_many
'''
my_col.insert_one({"name": "何", "age": 18, "sex": "男"})
my_col.insert_many([
{"name": "李", "age": 15, "sex": "男"},
{"name": "江", "age": 18, "sex": "女"},
{"name": "赵", "age": 12, "sex": "男"},
{"name": "刘", "age": 15, "sex": "女"},
])
'''
# 改 update_one update_many
'''
my_col.update_one({"age": 18}, {"$set": {"age": 16}}) # 记得加$set,否则会重写整条数据
my_col.update_many({"age": 12}, {"$set": {"age": 18}})
'''
# 删 delete_one delete_many
# my_col.delete_one({"sex": "男"})
# my_col.delete_many({"sex": "男"})
# 查 find_one find
# res = my_col.find_one()
res = my_col.find() # 地址,可迭代
for i in res:
print(i)