python教程
- Python教程
- Python 简介
- Python3 下载安装
- python基础语法
- Python基本数据类型
- Python数据类型转换
- Python解释器
- Python 注释
- Python运算符
- Python数字(Number)
- Python字符串
- Python列表
- Python元组
- Python3 字典
- Python集合
- Python条件控制
- Python循环语句
- Python编程第一步
- Python 推导式
- Python3 迭代器与生成器
- Python函数
- Python lambda(匿名函数)
- Python 装饰器
- Python数据结构
- Python3 模块
- Python __name__ 与 __main__
- Python输入和输出
- Python3 File(文件) 方法
- Python3 OS 文件/目录方法
- Python3 错误和异常
- Python3 面向对象
- Python3 命名空间和作用域
- Python3 标准库概览
- -----高级教程----------
- Python3 正则表达式
- Python CGI编程
- Python MySQL - mysql-connector 驱动
- Python3 MySQL 数据库连接 - PyMySQL 驱动
- Python3 网络编程
- Python3 SMTP发送邮件
- Python3 多线程
- Python3 XML 解析
- Python3 JSON 数据解析
- Python3 日期和时间
- Python MongoDB
- **Python Mongodb 插入文档
- **Python Mongodb 查询文档
- **Python Mongodb 修改文档
- **Python Mongodb 排序
- **Python Mongodb 删除数据
- Python urllib
- Python uWSGI 安装配置
- Python3 pip
- Anaconda 教程
- Python3 operator 模块
- Python math 模块
- Python requests 模块
- Python random 模块
- Python AI 绘画
- Python statistics 模块
- Python hashlib 模块
- Python 量化
- Python pyecharts 模块
- Python selenium 库
- Python 爬虫 - BeautifulSoup
- Python Scrapy 库
- Python Markdown 生成 HTML
- Python sys 模块
- Python Pickle 模块
- Python subprocess 模块
- Python queue 模块
- Python StringIO 模块
- Python logging 模块
- Python datetime 模块
- Python re 模块
- Python csv 模块
- Python threading 模块
- Python asyncio 模块
- Python PyQt
- **Python PyQt 常用组件
- **Python PyQt 布局管理
- **Python PyQt 信号与槽机制
MongoDB 中的一个文档类似 SQL 表中的一条记录。
插入集合
集合中插入文档使用 insert_one() 方法,该方法的第一参数是字典 name => value 对。
以下实例向 sites 集合中插入文档:
实例
#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["kuaikuaixuandb"] mycol = mydb["sites"] mydict = { "name": "RUNOOB", "alexa": "10000", "url": "https://www.kuaikuaixuan.com" } x = mycol.insert_one(mydict) print(x) print(x)
执行输出结果为:
返回 _id 字段
insert_one() 方法返回 InsertOneResult 对象,该对象包含 inserted_id 属性,它是插入文档的 id 值。
实例
#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/') mydb = myclient['kuaikuaixuandb'] mycol = mydb["sites"] mydict = { "name": "Google", "alexa": "1", "url": "https://www.google.com" } x = mycol.insert_one(mydict) print(x.inserted_id)
执行输出结果为:
如果我们在插入文档时没有指定 _id,MongoDB 会为每个文档添加一个唯一的 id。
插入多个文档
集合中插入多个文档使用 insert_many() 方法,该方法的第一参数是字典列表。
实例
#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["kuaikuaixuandb"] mycol = mydb["sites"] mylist = [ { "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" }, { "name": "QQ", "alexa": "101", "url": "https://www.qq.com" }, { "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" }, { "name": "知乎", "alexa": "103", "url": "https://www.zhihu.com" }, { "name": "Github", "alexa": "109", "url": "https://www.github.com" } ] x = mycol.insert_many(mylist) # 输出插入的所有文档对应的 _id 值 print(x.inserted_ids)
输出结果类似如下:
insert_many() 方法返回 InsertManyResult 对象,该对象包含 inserted_ids 属性,该属性保存着所有插入文档的 id 值。
执行完以上查找,我们可以在命令终端,查看数据是否已插入:
插入指定 _id 的多个文档
我们也可以自己指定 id,插入,以下实例我们在 site2 集合中插入数据,_id 为我们指定的:
实例
#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["kuaikuaixuandb"] mycol = mydb["site2"] mylist = [ { "_id": 1, "name": "RUNOOB", "cn_name": "饭桶教程"}, { "_id": 2, "name": "Google", "address": "Google 搜索"}, { "_id": 3, "name": "Facebook", "address": "脸书"}, { "_id": 4, "name": "Taobao", "address": "淘宝"}, { "_id": 5, "name": "Zhihu", "address": "知乎"} ] x = mycol.insert_many(mylist) # 输出插入的所有文档对应的 _id 值 print(x.inserted_ids)
输出结果为:
执行完以上查找,我们可以在命令终端,查看数据是否已插入:
© 2025 Copyright: kuaikuaixuan.com
京ICP备14015652号-3
网址导航