阅读本文大概需要5分钟
常用的数据库有三大类,MySQL,MongoDB和 Redis。Python对这三大数据库都有很好的支持。其中我个人比较喜欢Mongodb,简单快捷,操作方便。下面这篇文章对Mongodb常用的增删改查的操作,总结的非常到位。这篇是来自大名鼎鼎的爬虫大神崔庆才老师,我和他聊过几次,相谈甚欢,有一种相见恨晚的赶脚。崔老师的技术非常牛,欢迎关注他的,进击的Coder。
MongoDB是由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似JSON对象,它的字段值可以包含其他文档、数组及文档数组,非常灵活。在这一节中,我们就来看看Python 3下MongoDB的存储操作。
1.准备工作
在开始之前,请确保已经安装好了MongoDB并启动了其服务,并且安装好了Python的PyMongo库。
连接MongoDB时,我们需要使用PyMongo库里面的MongoClient
。一般来说,传入MongoDB的IP及端口即可,其中第一个参数为地址host
,第二个参数为端口port
(如果不给它传递参数,默认是27017):
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">import</span> pymongo<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">client = pymongo.MongoClient(host=<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'localhost'</span>, port=<span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">27017</span>)</span></p>
这样就可以创建MongoDB的连接对象了。
另外,MongoClient
的第一个参数host
还可以直接传入MongoDB的连接字符串,它以mongodb
开头,例如:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">client = MongoClient(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'mongodb://localhost:27017/'</span>)</span></p>
这也可以达到同样的连接效果。
2.指定数据集
MongoDB中可以建立多个数据库,接下来我们需要指定操作哪个数据库。这里我们以test数据库为例来说明,下一步需要在程序中指定要使用的数据库:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">db = client.test</span></p>
这里调用client
的test
属性即可返回test数据库。当然,我们也可以这样指定:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">db = client[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'test'</span>]</span></p>
这两种方式是等价的。
3.指定集合
MongoDB的每个数据库又包含许多集合(collection),它们类似于关系型数据库中的表。
下一步需要指定要操作的集合,这里指定一个集合名称为students。与指定数据库类似,指定集合也有两种方式:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">collection = db.students</span></p>
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">collection = db[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'students'</span>]</span></p>
这样我们便声明了一个Collection
对象。
4.插入数据
接下来,便可以插入数据了。对于students这个集合,新建一条学生数据,这条数据以字典形式表示:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">student = {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170101'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Jordan'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">}</span></p>
这里指定了学生的学号、姓名、年龄和性别。接下来,直接调用collection
的insert()
方法即可插入数据,代码如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">result = collection.insert(student) print(result)</span></p>
在MongoDB中,每条数据其实都有一个_id
属性来唯一标识。如果没有显式指明该属性,MongoDB会自动产生一个ObjectId
类型的_id
属性。insert()
方法会在执行后返回_id
值。
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">5932</span>a68615c2606814c91f3d</span></p>
当然,我们也可以同时插入多条数据,只需要以列表形式传递即可,示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">student1 = {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170101'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Jordan'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">student2 = {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170202'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Mike'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">21</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.insert([student1, student2])<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)</span></p>
返回结果是对应的_id
的集合:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">[ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'5932a80115c2606a59e8a048'</span>), ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'5932a80115c2606a59e8a049'</span>)]</span></p>
实际上,在PyMongo 3.x版本中,官方已经不推荐使用insert()
方法了。当然,继续使用也没有什么问题。官方推荐使用insert_one()
和insert_many()
方法来分别插入单条记录和多条记录,示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">student = {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170101'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Jordan'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.insert_one(student)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result.inserted_id)</span></p>
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><pymongo.results.InsertOneResult object at <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">0x10d68b558</span>><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">5932</span>ab0f15c2606f0c1cf6c5</span></p>
与insert()
方法不同,这次返回的是InsertOneResult
对象,我们可以调用其inserted_id
属性获取_id
。
对于insert_many()
方法,我们可以将数据以列表形式传递,示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">student1 = {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170101'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Jordan'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">student2 = {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170202'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Mike'</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">21</span>,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.insert_many([student1, student2])<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result.inserted_ids)</span></p>
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><pymongo.results.InsertManyResult object at <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">0x101dea558</span>><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">[ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'5932abf415c2607083d3b2ac'</span>), ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'5932abf415c2607083d3b2ad'</span>)]</span></p>
该方法返回的类型是InsertManyResult
,调用inserted_ids
属性可以获取插入数据的_id
列表。
5.查询数据库
插入数据后,我们可以利用find_one()
或find()
方法进行查询,其中find_one()
查询得到的是单个结果,find()
则返回一个生成器对象。示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">result = collection.find_one({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Mike'</span>})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(type(result))<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)</span></p>
这里我们查询name
为Mike
的数据,它的返回结果是字典类型,运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><class 'dict'><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">{'_id': ObjectId('5932a80115c2606a59e8a049'), 'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male'}</span></p>
可以发现,它多了_id
属性,这就是MongoDB在插入过程中自动添加的。
此外,我们也可以根据ObjectId
来查询,此时需要使用bson库里面的objectid
:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">from</span> bson.objectid <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">import</span> ObjectId<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.find_one({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'_id'</span>: ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'593278c115c2602667ec6bae'</span>)})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)</span></p>
其查询结果依然是字典类型,具体如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">{<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'_id'</span>: ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'593278c115c2602667ec6bae'</span>), <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170101'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Jordan'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span>}</span></p>
当然,如果查询结果不存在,则会返回None
。
对于多条数据的查询,我们可以使用find()
方法。例如,这里查找年龄为20的数据,示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">results = collection.find({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(results)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">for</span> result <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">in</span> results:<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"> print(result)</span></p>
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><pymongo.cursor.Cursor object at <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">0x1032d5128</span>><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">{<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'_id'</span>: ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'593278c115c2602667ec6bae'</span>), <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170101'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Jordan'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span>}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">{<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'_id'</span>: ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'593278c815c2602678bb2b8d'</span>), <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170102'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Kevin'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span>}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">{<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'_id'</span>: ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'593278d815c260269d7645a8'</span>), <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'id'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'20170103'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Harden'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'gender'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'male'</span>}</span></p>
返回结果是Cursor
类型,它相当于一个生成器,我们需要遍历取到所有的结果,其中每个结果都是字典类型。
如果要查询年龄大于20的数据,则写法如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">results = collection.find({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$gt'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>}})</span></p>
这里查询的条件键值已经不是单纯的数字了,而是一个字典,其键名为比较符号$gt
,意思是大于,键值为20。这里将比较符号归纳为下表。
另外,还可以进行正则匹配查询。例如,查询名字以M开头的学生数据,示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">results = collection.find({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$regex'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'^M.*'</span>}})</span></p>
这里使用$regex
来指定正则匹配,^M.*
代表以M开头的正则表达式。
这里将一些功能符号再归类为下表。
6.查询计数
要统计查询结果有多少条数据,可以调用count()
方法。比如,统计所有数据条数:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">count = collection.find().count() print(count)</span></p>
或者统计符合某个条件的数据:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">count = collection.find({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>}).count()<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(count)</span></p>
运行结果是一个数值,即符合条件的数据条数。
7.数据排序
排序时,直接调用sort()
方法,并在其中传入排序的字段及升降序标志即可。示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">results = collection.find().sort(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>, pymongo.ASCENDING)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print([result[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>] <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">for</span> result <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">in</span> results])</span></p>
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Harden'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Jordan'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Kevin'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Mark'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Mike'</span>]</span></p>
这里我们调用pymongo.ASCENDING
指定升序。如果要降序排列,可以传入pymongo.DESCENDING
。
8.数据的偏移
在某些情况下,我们可能想只取某几个元素,这时可以利用skip()
方法偏移几个位置,比如偏移2,就忽略前两个元素,得到第三个及以后的元素:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">results = collection.find().sort(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>, pymongo.ASCENDING).skip(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">2</span>)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print([result[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>] <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">for</span> result <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">in</span> results])</span></p>
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Kevin'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Mark'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Mike'</span>]</span></p>
另外,还可以用limit()
方法指定要取的结果个数,示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">results = collection.find().sort(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>, pymongo.ASCENDING).skip(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">2</span>).limit(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">2</span>)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print([result[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>] <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">for</span> result <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">in</span> results])</span></p>
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Kevin'</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Mark'</span>]</span></p>
如果不使用limit()
方法,原本会返回三个结果,加了限制后,会截取两个结果返回。
值得注意的是,在数据库数量非常庞大的时候,如千万、亿级别,最好不要使用大的偏移量来查询数据,因为这样很可能导致内存溢出。此时可以使用类似如下操作来查询:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">from</span> bson.objectid <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">import</span> ObjectId<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">collection.find({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'_id'</span>: {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$gt'</span>: ObjectId(<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'593278c815c2602678bb2b8d'</span>)}})</span></p>
这时需要记录好上次查询的_id
。
9.更新数据库
对于数据更新,我们可以使用update()
方法,指定更新的条件和更新后的数据即可。例如:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">condition = {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Kevin'</span>}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">student = collection.find_one(condition)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">student[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>] = <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">25</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.update(condition, student)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)</span></p>
这里我们要更新name
为Kevin
的数据的年龄:首先指定查询条件,然后将数据查询出来,修改年龄后调用update()
方法将原条件和修改后的数据传入。
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">{<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'ok'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'nModified'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'n'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'updatedExisting'</span>: <span class="" style="font-size: 15px;max-width: 100%;font-weight: bold;box-sizing: border-box !important;word-wrap: break-word !important;">True</span>}</span></p>
返回结果是字典形式,ok
代表执行成功,nModified
代表影响的数据条数。
另外,我们也可以使用$set
操作符对数据进行更新,代码如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">result = collection.update(condition, {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$set'</span>: student})</span></p>
这样可以只更新student
字典内存在的字段。如果原先还有其他字段,则不会更新,也不会删除。而如果不用$set
的话,则会把之前的数据全部用student
字典替换;如果原本存在其他字段,则会被删除。
另外,update()
方法其实也是官方不推荐使用的方法。这里也分为update_one()
方法和update_many()
方法,用法更加严格,它们的第二个参数需要使用$
类型操作符作为字典的键名,示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">condition = {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Kevin'</span>}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">student = collection.find_one(condition)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">student[<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>] = <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">26</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.update_one(condition, {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$set'</span>: student})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result.matched_count, result.modified_count)</span></p>
这里调用了update_one()
方法,第二个参数不能再直接传入修改后的字典,而是需要使用{'$set': student}
这样的形式,其返回结果是UpdateResult
类型。然后分别调用matched_count
和modified_count
属性,可以获得匹配的数据条数和影响的数据条数。
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><pymongo.results.UpdateResult object at <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">0x10d17b678</span>><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">0</span></span></p>
我们再看一个例子:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">condition = {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$gt'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>}}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.update_one(condition, {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$inc'</span>: {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span>}})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result.matched_count, result.modified_count)</span></p>
这里指定查询条件为年龄大于20,然后更新条件为{'$inc': {'age': 1}}
,也就是年龄加1,执行之后会将第一条符合条件的数据年龄加1。
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><pymongo.results.UpdateResult object at <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">0x10b8874c8</span>><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span></span></p>
可以看到匹配条数为1条,影响条数也为1条。
如果调用update_many()
方法,则会将所有符合条件的数据都更新,示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">condition = {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$gt'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">20</span>}}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.update_many(condition, {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$inc'</span>: {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span>}})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result.matched_count, result.modified_count)</span></p>
这时匹配条数就不再为1条了,运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><pymongo.results.UpdateResult object at <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">0x10c6384c8</span>><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">3</span> <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">3</span></span></p>
可以看到,这时所有匹配到的数据都会被更新。
10.数据删除
删除操作比较简单,直接调用remove()
方法指定删除的条件即可,此时符合条件的所有数据均会被删除。示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">result = collection.remove({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Kevin'</span>})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)</span></p>
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">{<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'ok'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span>, <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'n'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span>}</span></p>
另外,这里依然存在两个新的推荐方法——delete_one()
和delete_many()
。示例如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;">result = collection.delete_one({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'name'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'Kevin'</span>})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result.deleted_count)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">result = collection.delete_many({<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'age'</span>: {<span class="" style="font-size: 15px;max-width: 100%;color: rgb(221, 17, 68);box-sizing: border-box !important;word-wrap: break-word !important;">'$lt'</span>: <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">25</span>}})<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">print(result.deleted_count)</span></p>
运行结果如下:
<p style="padding: 0.5em;max-width: 100%;font-size: 0.85em;font-family: Consolas, Inconsolata, Courier, monospace;overflow: auto;border-radius: 3px;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;word-wrap: break-word !important;display: block !important;"><span style="font-size: 15px;"><pymongo.results.DeleteResult object at <span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">0x10e6ba4c8</span>><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">1</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span class="" style="font-size: 15px;max-width: 100%;color: rgb(0, 128, 128);box-sizing: border-box !important;word-wrap: break-word !important;">4</span></span></p>
delete_one()
即删除第一条符合条件的数据,delete_many()
即删除所有符合条件的数据。它们的返回结果都是DeleteResult
类型,可以调用deleted_count
属性获取删除的数据条数。
本文是对PyMongo的一个入门介绍,更多详细用法,可以参见官方文档:http://api.mongodb.com/python/current/api/pymongo/collection.html
另外,上期的送书调查结果(5月,送书10本|精选一份Python好书清单)有150多条留言,我正在用爬虫爬取大家的调查结果并数据分析,大家稍等一两天,我会尽快分析完毕,然后公布中奖结果。
近期热门:
5月,送书10本|精选一份Python好书清单
超酷,用Python还能写超级玛丽游戏
30岁后还能不能学Python,这里有一碗鸡汤
程序员如何用Python科学炒股
零基础学了8个月的Python,到底有啥感悟
本篇文章来源于: 菜鸟学Python
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 使用 Pycharm 中的数据库可视化数据库11/05
- ♥ 看完这篇Python装饰器详解,不知道怎么做就无能为力了12/04
- ♥ python中的socket建立客户端连接11/25
- ♥ Github上竟然有这么多可以帮你赚钱的项目07/08
- ♥ 如何在python中获取字典的键08/30
- ♥ 如何在python中编写for循环08/24
内容反馈