建立基本连接

首先我们需要建立一个连接,连接 MongoDB 时,我们需要使用 PyMongo 库中的 MongoClient 来建立连接,默认连接的地址是 mongodb://localhost:27017

1
2
3
4
5
from pymongo import MongoClient

clinet = MongoClient("mongodb://localhost:27017")
db = clinet["demo"]
col = db["demo"]

首先通过上面的代码创建 数据库对象和集合对象。

  • 数据库连接实例 MongoClient
  • 数据库实例 demo
  • 集合实例 demo

基本命令

查看数据库信息

1
server_info = clinet.server_info()

输出信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"version": "4.2.6",
"gitVersion": "20364840b8f1af16917e4c23c1b5f5efd8b352f8",
"modules": [],
"allocator": "tcmalloc",
"javascriptEngine": "mozjs",
"sysInfo": "deprecated",
"versionArray": [4, 2, 6, 0],
"openssl": {
"running": "OpenSSL 1.1.1 11 Sep 2018",
"compiled": "OpenSSL 1.1.1 11 Sep 2018"
},
"buildEnvironment": {
"distmod": "ubuntu1804",
"distarch": "x86_64",
"cc": "/opt/mongodbtoolchain/v3/bin/gcc: gcc (GCC) 8.2.0",
"ccflags": "-fno-omit-frame-pointer -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -O2 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-const-variable -Wno-unused-but-set-variable -Wno-missing-braces -fstack-protector-strong -fno-builtin-memcmp",
"cxx": "/opt/mongodbtoolchain/v3/bin/g++: g++ (GCC) 8.2.0",
"cxxflags": "-Woverloaded-virtual -Wno-maybe-uninitialized -fsized-deallocation -std=c++17",
"linkflags": "-pthread -Wl,-z,now -rdynamic -Wl,--fatal-warnings -fstack-protector-strong -fuse-ld=gold -Wl,--build-id -Wl,--hash-style=gnu -Wl,-z,noexecstack -Wl,--warn-execstack -Wl,-z,relro",
"target_arch": "x86_64",
"target_os": "linux"
},
"bits": 64,
"debug": false,
"maxBsonObjectSize": 16777216,
"storageEngines": ["biggie", "devnull", "ephemeralForTest", "wiredTiger"],
"ok": 1
}

显示当前数据库服务器上的数据库名

1
database_names = clinet.list_database_names()

输出信息:

1
['admin', 'config', 'demo', 'local']

如果没有 demo 数据库是因为在没有插入数据的情况下是不会被创建的,只有第一次插入数据,会自动的创建数据库以及对应的集合。

显示当前数据库上的全部集合名

1
collection_names = db.list_collection_names()

输出信息:

1
['demo']

插入文档

插入一个文档

1
2
3
4
5
6
7
demo = {
"author": "Sitoi",
"age": 22,
"title": "Sitoi-blog",
"tags": ["man", "spider"]
}
demo_id = col.insert_one(demo).inserted_id

输出信息:

1
5ee3806bb6c75d29c94aa9fc

插入多个文档

1
2
3
4
5
6
7
8
demos = [
{"author": "blog", "age": 18, "title": "blog", "text": "Sitoi Blog"},
{"author": "bash", "age": 30, "title": "bash", "text": "Sitoi Blog"},
{"author": "python", "age": 50, "title": "language", "text": "Sitoi Blog"},
{"author": "mongodb", "age": 80, "title": "NoSQL", "text": "Sitoi Blog"},
{"author": "pymongo", "age": 97, "title": "Python for MongoDB", "text": "Sitoi Blog"},
]
demo_ids = col.insert_many(demos).inserted_ids

输出信息:

1
[ObjectId('5ee3806bb6c75d29c94aa9fd'), ObjectId('5ee3806bb6c75d29c94aa9fe'), ObjectId('5ee3806bb6c75d29c94aa9ff'), ObjectId('5ee3806bb6c75d29c94aaa00'), ObjectId('5ee3806bb6c75d29c94aaa01')]

查询文档

查询单个文档

返回查询的第一条find_one 里面可以填写查询条件

参数说明:

  • filter:查询条件
  • projection:映射条件
1
2
3
query = {}
projection = None
result = col.find_one(filter=query, projection=projection)

查询结果:

1
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}

查询多个文档

返回一个对象,find 里面可以填写 查询条件

  • filter:查询条件
  • projection:映射条件
1
2
3
query = {}
projection = None
result = col.find(filter=query, projection=projection)

查询结果:

1
<pymongo.cursor.Cursor object at 0x7f695ed56c88>

通过For循环

1
2
3
results = col.find()
for result in results:
print(result)

遍历结果:

1
2
3
4
5
6
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fe'), 'author': 'bash', 'age': 30, 'title': 'bash', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9ff'), 'author': 'python', 'age': 50, 'title': 'language', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa00'), 'author': 'mongodb', 'age': 80, 'title': 'NoSQL', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa01'), 'author': 'pymongo', 'age': 97, 'title': 'Python for MongoDB', 'text': 'Sitoi Blog'}

指定返回哪些字段

通过 projection 参数控制返回的结果包含哪些字段

示例一:所有字段

1
results = col.find()

查询结果:

1
2
3
4
5
6
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fe'), 'author': 'bash', 'age': 30, 'title': 'bash', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9ff'), 'author': 'python', 'age': 50, 'title': 'language', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa00'), 'author': 'mongodb', 'age': 80, 'title': 'NoSQL', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa01'), 'author': 'pymongo', 'age': 97, 'title': 'Python for MongoDB', 'text': 'Sitoi Blog'}

示例二:用字典指定要显示的哪几个字段

1
2
3
query = {}
projection = {"_id": True, "author": True}
results = col.find(filter=query, projection=projection)

查询结果:

1
2
3
4
5
6
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fe'), 'author': 'bash'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9ff'), 'author': 'python'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa00'), 'author': 'mongodb'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa01'), 'author': 'pymongo'}

示例三:用字典指定去掉哪些字段

1
2
3
query = {}
projection = {"_id": False, "author": False}
results = col.find(query, projection=projection)

查询结果:

1
2
3
4
5
6
{'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}
{'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}
{'age': 30, 'title': 'bash', 'text': 'Sitoi Blog'}
{'age': 50, 'title': 'language', 'text': 'Sitoi Blog'}
{'age': 80, 'title': 'NoSQL', 'text': 'Sitoi Blog'}
{'age': 97, 'title': 'Python for MongoDB', 'text': 'Sitoi Blog'}

示例四:用列表指定要显示哪几个字段

_id 不指定为 False 则必定返回

1
2
3
query = {}
projection = ["author", "title"]
results = col.find(filter=query, projection=projection)

查询结果:

1
2
3
4
5
6
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'title': 'Sitoi-blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'title': 'blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fe'), 'author': 'bash', 'title': 'bash'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9ff'), 'author': 'python', 'title': 'language'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa00'), 'author': 'mongodb', 'title': 'NoSQL'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa01'), 'author': 'pymongo', 'title': 'Python for MongoDB'}

指定查询条件

符号 含义 示例
$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

1
2
3
query = {"age": {"$gte": 10, "$lte": 30}}
projection = None
results = col.find(filter=query, projection=projection)

查询结果:

1
2
3
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fe'), 'author': 'bash', 'age': 30, 'title': 'bash', 'text': 'Sitoi Blog'}

并列查询

示例一:不同字段,并列条件

1
2
3
query = {"author": "Sitoi", "age": 22}
projection = None
results = col.find(filter=query, projection=projection)

查询结果:

1
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}

示例二:相同字段,并列条件

1
2
3
4
5
6
# 错误:
query = {"age": {"$gt": 10}, "age": {"$lt": 20}}
# 正确:
query = {"age": {"$gte": 10, "$lte": 20}}
projection = None
results = col.find(filter=query, projection=projection)

查询结果:

1
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}

或条件查询

示例一:不同字段,或条件

1
2
3
query = {"$or": [{"age": 22}, {"author": "blog"}]}
projection = None
results = col.find(filter=query, projection=projection)

查询结果:

1
2
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}

示例二:相同字段,或条件

1
2
3
query = {"$or": [{"age": 22}, {"age": 18}]}
projection = None
results = col.find(filter=query, projection=projection)

查询结果:

1
2
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}

字段是否存在

示例一:字段不存在

1
2
3
query = {"text": None}
projection = None
results = col.find(filter=query, projection=projection)

查询结果:

1
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}

示例二:字段存在

1
2
3
query = {"text": {"$ne": None}}
projection = None
results = col.find(filter=query, projection=projection)

查询结果:

1
2
3
4
5
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fe'), 'author': 'bash', 'age': 30, 'title': 'bash', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9ff'), 'author': 'python', 'age': 50, 'title': 'language', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa00'), 'author': 'mongodb', 'age': 80, 'title': 'NoSQL', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa01'), 'author': 'pymongo', 'age': 97, 'title': 'Python for MongoDB', 'text': 'Sitoi Blog'}

正则查询

这里使用 $regex 来指定正则匹配,^S.* 代表以 S 开头的正则表达式。
这里将一些功能符号再归类为下表。

符号 含义 示例 示例含义
$regex 匹配正则表达式 {'author': {'$regex': '^S.*'}} authorS 开头
$exists 属性是否存在 {'author': {'$exists': True}} author 属性存在
$type 类型判断 {'age': {'$type': 'int'}} age 的类型为 int
$mod 数字模操作 {'age': {'$mod': [5, 0]}} 年龄模 50
$text 文本查询 {'$text': {'$search': 'Sitoi'}} text 类型的属性中包含 Sitoi 字符串
$where 高级条件查询 {'$where': 'obj.fans_count == obj.follows_count'} 自身粉丝数等于关注数
1
2
3
query = {"name": {"$regex": "^M.*"}}
projection = {}
result = col.find(filter=query, projection=projection)

查询结果:

1
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}

计数

根据查询条件计数

1
2
query = {}
count = col.count_documents(filter=query)

文档条数:

1
6

排序

List 嵌套 tuple 的方式即可:[(字段名 1排序方式 1),(字段名 2排序方式 2)]

1
result = col.find().sort([("author", 1), ("title", 1)])

排序结果:

1
2
3
4
5
6
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fe'), 'author': 'bash', 'age': 30, 'title': 'bash', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa00'), 'author': 'mongodb', 'age': 80, 'title': 'NoSQL', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa01'), 'author': 'pymongo', 'age': 97, 'title': 'Python for MongoDB', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9ff'), 'author': 'python', 'age': 50, 'title': 'language', 'text': 'Sitoi Blog'}

跳过

跳过一个

1
result = col.find().skip(1)

查询结果:

1
2
3
4
5
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fd'), 'author': 'blog', 'age': 18, 'title': 'blog', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fe'), 'author': 'bash', 'age': 30, 'title': 'bash', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aa9ff'), 'author': 'python', 'age': 50, 'title': 'language', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa00'), 'author': 'mongodb', 'age': 80, 'title': 'NoSQL', 'text': 'Sitoi Blog'}
{'_id': ObjectId('5ee3806bb6c75d29c94aaa01'), 'author': 'pymongo', 'age': 97, 'title': 'Python for MongoDB', 'text': 'Sitoi Blog'}

限制

限制最多返回多少个

1
result = col.find().limit(1)

查询结果:

1
{'_id': ObjectId('5ee3806bb6c75d29c94aa9fc'), 'author': 'Sitoi', 'age': 22, 'title': 'Sitoi-blog', 'tags': ['man', 'spider']}

更新文档

更新单个文档

update_one 只更新第一个文档。

参数说明:

  • filter:需要更新的数据的查询条件
  • update:包含更新的方式,以及更新的内容
  • upsert:不存在是否插入,更新的数据
1
2
3
query = {"age": 18}
update = {"$set": {"age": 20}}
modified_count = col.update_one(filter=query, update=update, upsert=False).modified_count

更新条数:

1
1

更新多个文档

使用方法和 update_one 一致

1
2
3
query = {"text": "Sitoi Blog"}
update = {"$set": {"text": "Sitoi PyMongo"}}
modified_count = col.update_many(filter=query, update=update, upsert=False).modified_count

更新条数:

1
5

删除文档

删除单个文档

1
2
query = {"author": "Sitoi"}
result = col.delete_one(filter=query).deleted_count

删除条数:

1
1

删除多个文档

1
2
query = {}
results = col.delete_many(filter=query).deleted_count

删除条数:

1
5

附录(代码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from pymongo import MongoClient

clinet = MongoClient("mongodb://localhost:27017")

db = clinet["demo"]
col = db["demo"]

print(">>> 查看数据库信息")
server_info = clinet.server_info()
print(server_info)

print(">>> 显示当前数据库服务器上的数据库名")
database_names = clinet.list_database_names()
print(database_names)

print(">>> 显示当前数据库上的全部集合名")
collection_names = db.list_collection_names()
print(collection_names)

print(">>> 插入一个文档")
demo = {
"author": "Sitoi",
"age": 22,
"title": "Sitoi-blog",
"tags": ["man", "spider"]
}
demo_id = col.insert_one(demo).inserted_id
print(demo_id)

print(">>> 插入多个文档")
demos = [
{"author": "blog", "age": 18, "title": "blog", "text": "Sitoi Blog"},
{"author": "bash", "age": 30, "title": "bash", "text": "Sitoi Blog"},
{"author": "python", "age": 50, "title": "language", "text": "Sitoi Blog"},
{"author": "mongodb", "age": 80, "title": "NoSQL", "text": "Sitoi Blog"},
{"author": "pymongo", "age": 97, "title": "Python for MongoDB", "text": "Sitoi Blog"},
]
demo_ids = col.insert_many(demos).inserted_ids
print(demo_ids)

print(">>> 查询单个文档")
query = {}
projection = None
result = col.find_one(filter=query, projection=projection)
print(result)

print(">>> 查询多个文档")
query = {}
projection = None
result = col.find(filter=query, projection=projection)
for one in result:
print(one)

print(">>> 所有字段")

results = col.find()
for one in results:
print(one)

print(">>> 用字典指定要显示的哪几个字段")
query = {}
projection = {"_id": True, "author": True}
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 用字典指定去掉哪些字段")
query = {}
projection = {"_id": False, "author": False}
results = col.find(query, projection=projection)
for one in results:
print(one)

print(">>> 用列表指定要显示哪几个字段")
query = {}
projection = ["author", "title"]
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 指定范围,大于等于,小于等于")
query = {"age": {"$gte": 10, "$lte": 30}}
projection = None
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 不同字段,并列条件")
query = {"author": "Sitoi", "age": 22}
projection = None
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 相同字段,并列条件")
# 错误:
query = {"age": {"$gt": 50}, "age": {"$lt": 100}}
# 正确:
query = {"age": {"$gte": 10, "$lte": 20}}
projection = None
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 不同字段,或条件")
query = {"$or": [{"age": 22}, {"author": "blog"}]}
projection = None
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 相同字段,或条件")
query = {"$or": [{"age": 22}, {"age": 18}]}
projection = None
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 字段不存在")
query = {"text": None}
projection = None
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 字段存在")
query = {"text": {"$ne": None}}
projection = None
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 正则查询")
query = {"author": {"$regex": "^S.*"}}
projection = None
results = col.find(filter=query, projection=projection)
for one in results:
print(one)

print(">>> 计数")
query = {}
count = col.count_documents(filter=query)
print(count)

print(">>> 排序")
results = col.find().sort([("author", 1), ("title", -1)])
for one in results:
print(one)

print(">>> 跳过")
results = col.find().skip(1)
for one in results:
print(one)

print(">>> 限制")
results = col.find().limit(1)
for one in results:
print(one)

print(">>> 更新单个文档")
query = {"age": 18}
update = {"$set": {"age": 20}}
modified_count = col.update_one(filter=query, update=update, upsert=False).modified_count
print(modified_count)

print(">>> 更新多个文档")
query = {"text": "Sitoi Blog"}
update = {"$set": {"text": "Sitoi PyMongo"}}
modified_count = col.update_many(filter=query, update=update, upsert=False).modified_count
print(modified_count)

print(">>> 删除单个文档")
query = {"author": "Sitoi"}
delete_count = col.delete_one(filter=query).deleted_count
print(delete_count)

print(">>> 删除多个文档")
query = {}
delete_count = col.delete_many(filter=query).deleted_count
print(delete_count)