mongodb和python交互

1. mongdb和python交互的模块

pymongo 提供了mongdb和python交互的所有方法

安装方式: pip install pymongo

2. 使用pymongo

2.1 导入pymongo并选择要操作的集合

数据库和集合能够自动创建

2.1.1 无需权限认证的方式创建连接对象以及集合操作对象
python 复制代码
from pymongo import MongoClient

client = MongoClient(host,port) # 如果是本地连接host,port参数可以省略

collection = client[db名][集合名]
# collection = client.db名.集合名 # 与上边用法相同
2.1.2 需要权限认证的方式创建连接对象以及集合操作对象
python 复制代码
from pymongo import MongoClient
from urllib.parse import quote_plus

user = 'python' # 账号
password = 'python' # 密码
host = '127.0.0.1' # host
port = 27017 # port
uri = "mongodb://%s:%s@%s" % (quote_plus(user),
                              quote_plus(password),
                              host)
# quote_plus函数:对url进行编码
# uri = mongodb://python:python@127.0.0.1
client = MongoClient(uri, port=port)
collection = client.db名.集合名
2.2 insert()添加数据

insert可以批量的插入数据列表,也可以插入一条数据

python 复制代码
collection.insert({一条数据})
collection.insert([{数据一},{数据二}])
2.2.1 添加一条数据

返回插入数据的_id

python 复制代码
ret = collection.insert({"name":"test10010","age":33})
print(ret)
2.2.2 添加多条数据

返回ObjectId对象构成的列表

python 复制代码
item_list = [{"name":"test1000{}".format(i)} for i in range(10)]
rets = collection.insert(item_list)
print(rets)
for ret in rets:
    print(ret)
2.3 find_one()查找一条数据

接收一个字典形式的条件,返回字典形式的整条数据

如果条件为空,则返回第一条

python 复制代码
ret = client.test.test.find_one({'name': 'test10001'})
print(ret) # 包含mongodb的ObjectId对象的字典
_ = ret.pop('_id') # 清除mongodb的ObjectId对象的k,v
print(ret) 
2.4 find()查找全部数据

返回所有满足条件的结果,如果条件为空,则返回全部

结果是一个Cursor游标对象,是一个可迭代对象,可以类似读文件的指针,但是只能够进行一次读取

python 复制代码
rets = collection.find({"name":"test10005"}),
for ret in rets:
    print(ret)
for ret in rets: #此时rets中没有内容
    print(ret)
2.5 update()更新数据(全文档覆盖或指定键值,更新一条或多条)
  • 语法:collection.update({条件}, {'$set':{指定的kv或完整的一条数据}}, multi=False/True, upsert=False/True)
  • multi参数:默认为False,表示更新一条; multi=True则更新多条; multi参数必须和$set一起使用
  • upsert参数:默认为False; upsert=True则先查询是否存在,存在则更新;不存在就插入
  • $set表示指定字段进行更新
2.5.1 更新一条数据;全文档覆盖;存在就更新,不存在就插入
python 复制代码
data = {'msg':'这是一条完整的数据1','name':'哈哈'}
client.test.test.update({'haha': 'heihei'}, {'$set':data}, upsert=True)
2.5.2 更新多条数据;全文档覆盖;存在就更新,不存在就插入
python 复制代码
data = {'msg':'这是一条完整的数据2','name':'哈哈'} # 该完整数据是先查询后获取的
client.test.test.update({}, {'$set':data}, multi=True, upsert=True)
2.5.3 更新一条数据;指定键值;存在就更新,不存在就插入
python 复制代码
data = {'msg':'指定只更新msg___1'}
client.test.test.update({}, {'$set':data}, upsert=True)
2.5.4 更新多条数据;指定键值;存在就更新,不存在就插入
python 复制代码
data = {'msg':'指定只更新msg___2'}
client.test.test.update({}, {'$set':data}, multi=True, upsert=True)
2.6 delete_one()删除一条数据
python 复制代码
collection.delete_one({"name":"test10010"})
2.7 delete_many()删除全部数据
python 复制代码
collection.delete_many({"name":"test10010"})

3. pymongo模块其他api

查看pymongo官方文档或源代码 http://api.mongodb.com/python/current/

相关推荐
用户83562907805117 小时前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
用户83562907805119 小时前
用 Python 自动化 PowerPoint 演讲者备注添加
后端·python
黄忠1 天前
01-系统架构设计-LangGraph状态机与多源异构RAG
python
zzzzzz3101 天前
假如我是掘金管理员,我先给评论区装个'代码审查'系统
python·程序员·机器人
砍材农夫1 天前
python环境|conda安装和使用(2)
后端·python
程序员龙叔2 天前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户8356290780512 天前
使用 Python 操作 Word 内容控件
后端·python
✎ ﹏梦醒͜ღ҉繁华落℘2 天前
单片机基础知识---stm32单片机的优先级
stm32·单片机·mongodb
码云骑士2 天前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python