Python Data Persistence 简明教程
Python Data Persistence - PyMongo module
MongoDB 是一个面向 NoSQL 数据库的文档。它是一个在服务端公共许可证下分发的跨平台数据库。它使用类似 JSON 的文档作为架构。
MongoDB is a document oriented NoSQL database. It is a cross platform database distributed under server side public license. It uses JSON like documents as schema.
为了提供存储海量数据的能力,超过一个物理服务器(称为分片)相互连接,从而实现横向可扩展性。MongoDB 数据库由文档组成。
In order to provide capability to store huge data, more than one physical servers (called shards) are interconnected, so that a horizontal scalability is achieved. MongoDB database consists of documents.

文档类似于关系数据库中的表中的行。但是,它没有特定的模式。文档是键值对的集合——类似于字典。但是,每个文档中的键值对的数量可能有所不同。就像关系数据库中的表具有主键一样,MongoDB 数据库中的文档具有一个称为 "_id" 的特殊键。
A document is analogous to a row in a table of relational database. However, it doesn’t have a particular schema. Document is a collection of key-value pairs - similar to dictionary. However, number of k-v pairs in each document may vary. Just as a table in relational database has a primary key, document in MongoDB database has a special key called "_id".
在了解如何将 MongoDB 数据库与 Python 一起使用之前,让我们简要了解如何安装和启动 MongoDB。MongoDB 提供社区版和商业版。社区版本可以从 www.mongodb.com/download-center/community 下载。
Before we see how MongoDB database is used with Python, let us briefly understand how to install and start MongoDB. Community and commercial version of MongoDB is available. Community version can be downloaded from www.mongodb.com/download-center/community.
假设 MongoDB 安装在 c:\mongodb 中,则可以使用以下命令调用服务器。
Assuming that MongoDB is installed in c:\mongodb, the server can be invoked using following command.
c:\mongodb\bin>mongod
MongoDB 服务器默认在端口号 22017 上处于活动状态。数据库默认存储在 data/bin 文件夹中,尽管可以通过 –dbpath 选项更改位置。
The MongoDB server is active at port number 22017 by default. Databases are stored in data/bin folder by default, although the location can be changed by –dbpath option.
MongoDB 有一组自己的命令,可在 MongoDB shell 中使用。要调用 shell,请使用 Mongo 命令。
MongoDB has its own set of commands to be used in a MongoDB shell. To invoke shell, use Mongo command.
x:\mongodb\bin>mongo
类似于 MySQL 或 SQLite shell 提示符的 shell 提示符在可以使用原生 NoSQL 命令之前出现。但是,我们有兴趣将 MongoDB 数据库连接到 Python。
A shell prompt similar to MySQL or SQLite shell prompt, appears before which native NoSQL commands can be executed. However, we are interested in connecting MongoDB database to Python.
PyMongo 模块是由 MongoDB Inc 本身开发的,用于提供 Python 编程接口。使用众所周知的 pip 实用程序来安装 PyMongo。
PyMongo module has been developed by MongoDB Inc itself to provide Python programming interface. Use well known pip utility to install PyMongo.
pip3 install pymongo
假设 MongoDB 服务器已启动并正在运行(使用 mongod 命令),并正在监听端口 22017,我们首先需要声明一个 MongoClient 对象。它控制 Python 会话和数据库之间的所有事务。
Assuming that MongoDB server is up and running (with mongod command) and is listening at port 22017, we first need to declare a MongoClient object. It controls all transactions between Python session and the database.
from pymongo import MongoClient
client=MongoClient()
使用此客户端对象与 MongoDB 服务器建立连接。
Use this client object to establish connection with MongoDB server.
client = MongoClient('localhost', 27017)
使用以下命令创建新数据库。
A new database is created with following command.
db=client.newdb
MongoDB 数据库可以有许多集合,类似于关系数据库中的表。集合对象由 Create_collection() 函数创建。
MongoDB database can have many collections, similar to tables in a relational database. A Collection object is created by Create_collection() function.
db.create_collection('students')
现在,我们可以按如下方式在集合中添加一个或多个文档:-
Now, we can add one or more documents in the collection as follows −
from pymongo import MongoClient
client=MongoClient()
db=client.newdb
db.create_collection("students")
student=db['students']
studentlist=[{'studentID':1,'Name':'Juhi','age':20, 'marks'=100},
{'studentID':2,'Name':'dilip','age':20, 'marks'=110},
{'studentID':3,'Name':'jeevan','age':24, 'marks'=145}]
student.insert_many(studentlist)
client.close()
要检索文档(类似于 SELECT 查询),我们应当使用 find() 方法。它返回光标,它有助于获取所有文档。
To retrieve the documents (similar to SELECT query), we should use find() method. It returns a cursor with the help of which all documents can be obtained.
students=db['students']
docs=students.find()
for doc in docs:
print (doc['Name'], doc['age'], doc['marks'] )
要在集合中查找特定文档而不是所有文档,我们需要对 find() 方法应用过滤器来查找。过滤器使用逻辑运算符。MongoDB 有一套自己的逻辑运算符,如下所示 −
To find a particular document instead of all of them in a collection, we need to apply filter to find() method. The filter uses logical operators. MongoDB has its own set of logical operators as below −
Sr.No |
MongoDB operator & Traditional logical operator |
1 |
$eq equal to (==) |
2 |
$gt greater than (>) |
3 |
$gte greater than or equal to (>=) |
4 |
$in if equal to any value in array |
5 |
$lt less than (<) |
6 |
$lte less than or equal to (⇐) |
7 |
$ne not equal to (!=) |
8 |
$nin if not equal to any value in array |
例如,我们有兴趣获取大于 21 岁的学生列表。在 find() 方法的过滤器中使用 $gt 运算符,如下所示 −
For example, we are interested in obtaining list of students older than 21 years. Using $gt operator in the filter for find() method as follows −
students=db['students']
docs=students.find({'age':{'$gt':21}})
for doc in docs:
print (doc.get('Name'), doc.get('age'), doc.get('marks'))
PyMongo 模块提供 update_one() 和 update_many() 方法,以修改一个文档或多个满足特定过滤器表达式的文档。
PyMongo module provides update_one() and update_many() methods for modifying one document or more than one documents satisfying a specific filter expression.
让我们更新名为 Juhi 的文档的 marks 属性。
Let us update marks attribute of a document in which name is Juhi.
from pymongo import MongoClient
client=MongoClient()
db=client.newdb
doc=db.students.find_one({'Name': 'Juhi'})
db['students'].update_one({'Name': 'Juhi'},{"$set":{'marks':150}})
client.close()