PyMongo 常用语法汇总
建立基本连接
首先我们需要建立一个连接,连接 MongoDB 时,我们需要使用 PyMongo 库中的 MongoClient 来建立连接,默认连接的地址是 mongodb://localhost:27017
python
1 | from pymongo import MongoClient |
首先通过上面的代码创建 数据库对象和集合对象。
- 数据库连接实例
MongoClient
- 数据库实例
demo
- 集合实例
demo
基本命令
查看数据库信息
python
1 | server_info = clinet.server_info() |
输出信息:
json
1 | { |
显示当前数据库服务器上的数据库名
python
1 | database_names = clinet.list_database_names() |
输出信息:
plaintext
1 | ['admin', 'config', 'demo', 'local'] |
如果没有
demo
数据库是因为在没有插入数据的情况下是不会被创建的,只有第一次插入数据,会自动的创建数据库以及对应的集合。
显示当前数据库上的全部集合名
python
1 | collection_names = db.list_collection_names() |
输出信息:
plaintext
1 | ['demo'] |
插入文档
插入一个文档
python
1 | demo = { |
输出信息:
python
1 | 5ee3806bb6c75d29c94aa9fc |
插入多个文档
python
1 | demos = [ |
输出信息:
plaintext
1 | [ObjectId('5ee3806bb6c75d29c94aa9fd'), ObjectId('5ee3806bb6c75d29c94aa9fe'), ObjectId('5ee3806bb6c75d29c94aa9ff'), ObjectId('5ee3806bb6c75d29c94aaa00'), ObjectId('5ee3806bb6c75d29c94aaa01')] |
查询文档
查询单个文档
返回查询的第一条
,find_one
里面可以填写查询条件
参数说明:
filter
:查询条件projection
:映射条件
python
1 | query = {} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
查询多个文档
返回一个对象,find
里面可以填写 查询条件
filter
:查询条件projection
:映射条件
python
1 | query = {} |
查询结果:
plaintext
1 | <pymongo.cursor.Cursor object at 0x7f695ed56c88> |
通过
For
循环
python
1 | results = col.find() |
遍历结果:
python
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
指定返回哪些字段
通过 projection
参数控制返回的结果包含哪些字段
示例一:所有字段
python
1 | results = col.find() |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
示例二:用字典指定要显示的哪几个字段
python
1 | query = {} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi'} |
示例三:用字典指定去掉哪些字段
python
1 | query = {} |
查询结果:
plaintext
1 | {'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
示例四:用列表指定要显示哪几个字段
_id
不指定为False
则必定返回
python
1 | query = {} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'title': 'Sitoi-blog'} |
指定查询条件
符号 | 含义 | 示例 |
---|---|---|
$lt | 小于 | {‘age’: {‘$lt’: 18}} |
$gt | 大于 | {‘age’: {‘$gt’: 18}} |
$lte | 小于等于 | {‘age’: {‘$lte’: 18}} |
$gte | 大于等于 | {‘age’: {‘$gte’: 18}} |
$ne | 不等于 | {‘age’: {‘$ne’: 18}} |
$in | 在范围内 | {‘age’: {‘$in’: [18, 22]}} |
$nin | 不在范围内 | {‘age’: {‘$nin’: [18, 22]}} |
$all | 条件内所有值 | {‘age’: {‘$all’: [18, 22]}} |
示例:指定范围,大于等于,小于等于
10 <= 年龄 <= 30
python
1 | query = {"age": {"$gte": 10, "$lte": 30}} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
并列查询
示例一:不同字段,并列条件
python
1 | query = {"author": "Sitoi", "age": 22} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
示例二:相同字段,并列条件
python
1 | # 错误: |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'} |
或条件查询
示例一:不同字段,或条件
python
1 | query = {"$or": [{"age": 22}, {"author": "blog"}]} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
示例二:相同字段,或条件
python
1 | query = {"$or": [{"age": 22}, {"age": 18}]} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
字段是否存在
示例一:字段不存在
python
1 | query = {"text": None} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
示例二:字段存在
python
1 | query = {"text": {"$ne": None}} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'} |
正则查询
这里使用 $regex
来指定正则匹配,^S.*
代表以 S
开头的正则表达式。
这里将一些功能符号再归类为下表。
符号 | 含义 | 示例 | 示例含义 |
---|---|---|---|
$regex | 匹配正则表达式 | {'author': {'$regex': '^S.*'}} |
author 以 S 开头 |
$exists | 属性是否存在 | {'author': {'$exists': True}} |
author 属性存在 |
$type | 类型判断 | {'age': {'$type': 'int'}} |
age 的类型为 int |
$mod | 数字模操作 | {'age': {'$mod': [5, 0]}} |
年龄模 5 余 0 |
$text | 文本查询 | {'$text': {'$search': 'Sitoi'}} |
text 类型的属性中包含 Sitoi 字符串 |
$where | 高级条件查询 | {'$where': 'obj.fans_count == obj.follows_count'} |
自身粉丝数等于关注数 |
python
1 | query = {"name": {"$regex": "^M.*"}} |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
计数
根据查询条件计数
python
1 | query = {} |
文档条数:
plaintext
1 | 6 |
排序
用 List
嵌套 tuple
的方式即可:[(字段名1
,排序方式1
),(字段名2
,排序方式2
)]
python
1 | result = col.find().sort([("author", 1), ("title", 1)]) |
排序结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
跳过
跳过一个
python
1 | result = col.find().skip(1) |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'} |
限制
限制最多返回多少个
python
1 | result = col.find().limit(1) |
查询结果:
plaintext
1 | {'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']} |
更新文档
更新单个文档
update_one
只更新第一个文档。
参数说明:
filter
:需要更新的数据的查询条件update
:包含更新的方式,以及更新的内容upsert
:不存在是否插入,更新的数据
python
1 | query = {"age": 18} |
更新条数:
plaintext
1 | 1 |
更新多个文档
使用方法和
update_one
一致
python
1 | query = {"text": "Sitoi Blog"} |
更新条数:
plaintext
1 | 5 |
删除文档
删除单个文档
python
1 | query = {"author": "Sitoi"} |
删除条数:
plaintext
1 | 1 |
删除多个文档
python
1 | query = {} |
删除条数:
plaintext
1 | 5 |
附录(代码)
python
1 | from pymongo import MongoClient |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Sitoi!
评论