pymongo直接使用pip安装:pip install pymongo
示例代码
from pymongo import MongoClient
mongo_conn = MongoClient(host='localhost', port=27017)
mongo_db = mongo_conn.get_database("test") # 指定数据库
coll = mongo_db.get_collection("people") # 指定集合
new_people = {'name': "小明", 'age': 13}
# 增加
coll.insert_one(new_people)
coll.insert_many([{"name": "小红", 'age': 11}, {"name": "小蓝", "age": 12}])
# 查询
record = coll.find_one({'name': "小明"})
# 修改
coll.update_one(record, {
"$set": {"age": 22, '成绩': 101}, # "$set"是固定写法,后面字典里的是覆盖的新值
})
# 删除
coll.delete_one({"name": "小红"})