数据库的操作无非是要掌握数据库的增,改,删,此篇文章为查阅官方文档的翻译和解释。
官方文档:Tutorial — PyMongo 3.7.1 documentation http://api.mongodb.com/python/current/tutorial.html
Prerequisites先决条件
已经安装了pymongo,并且import pymongo
没有报错
(pymongo安装:在cmd中输入pip3 install pymongo
)
Making a Connection with MongoClient
连接MongoDB实例的客户端
1.from pymongo import MongoClient
没有添加参数时,会连接默认的用户和端口
client = MongoClient()
2.但是可以指定用户和端口client = MongoClient('localhost', 27017)
3.或者用MongoDB URI 形式
client = MongoClient('mongodb://localhost:27017/')
Getting a Database得到数据库
db = client.test_database
如果数据库名称不能直接用客户端.数据名的方式引用,如test-database
那就使用字典的方式引用:db = client['test-database']
Getting a Collection得到集合
集合(Collection)是MongoDB中的一系列文件,可以理解为关系数据库中的一个表。得到Collection和得到数据库的操作是一样的
collection = db.test_collection
或者collection = db['test-collection']
注:1.从Getting a Database和Getting a Collection中可以看出,你要先得到一个数据库(db是得到的数据库,collection是得到的集合),才能得到数据库中的集合。2.一旦有文档插入的时候集合和数据库就被创建
Documents
MongoDB中的数据都是以JSON的形式存储和展现的,使用PyMongo时是用字典展示文档的
import datetime
post = {"author": "Mike",
"text": "My first blog post!",
"tags": ["mongodb", "python", "pymongo"],
"date": datetime.datetime.utcnow()}
Inserting a Document插入单个文档
把一个文档插入一个集合中使用 insert_one()
>>> posts = db.posts
#insert_one插入文档,并用inserted_id得到id属性
>>> post_id = posts.insert_one(post).inserted_id
>>> post_id
ObjectId('...')
如果你要插入的文档字典中没有”_id”这个键,它会为你自动生成”_id”键和对应的值,每个文档都有自己特别的id。如图,椭圆中的是每个文档的_id,矩形中的表示一个文档
注:只要插入了第一个文档集合就随之创建通过“数据库名.collection_names()”方法可以查看数据库中所有的集合
>>> db.collection_names(include_system_collections=False)
[u'posts']
访问单个文档
Getting a Single Document With find_one()
得到单个文档的方法find_one(),结果返回None或者相匹配的文档
>>> import pprint
#没有已知信息匹配,返回了刚刚插入的posts
>>> pprint.pprint(posts.find_one())
{u'_id': ObjectId('...'),#_id是插入时MongoDB自动添加的
u'author': u'Mike',#u'author'而不是'author'因为PyMongo解码成unicode string
u'date': datetime.datetime(...),
u'tags': [u'mongodb', u'python', u'pymongo'],
u'text': u'My first blog post!'}
#用已知信息"author": "Mike"做匹配
>>> pprint.pprint(posts.find_one({"author": "Mike"}))
{u'_id': ObjectId('...'),
u'author': u'Mike',
u'date': datetime.datetime(...),
u'tags': [u'mongodb', u'python', u'pymongo'],
u'text': u'My first blog post!'}
Querying By ObjectId通过文档id访问文档
>>> post_id#文档id,见Inserting a Document
ObjectId(...)
>>> pprint.pprint(posts.find_one({"_id": post_id}))
{u'_id': ObjectId('...'),
u'author': u'Mike',
u'date': datetime.datetime(...),
u'tags': [u'mongodb', u'python', u'pymongo'],
u'text': u'My first blog post!'}
>>> post_id_as_str = str(post_id)#文档id可转化为字符串形式,但是不能用该形式匹配文档
>>> posts.find_one({"_id": post_id_as_str}) # No result没有返回结果
>>>
如果有str类型的id务必在使用find_one方法前改为ObjectId类型
from bson.objectid import ObjectId
# The web framework gets post_id from the URL and passes it as a string
def get(post_id):
# Convert from string to ObjectId:
document = client.db.collection.find_one({'_id': ObjectId(post_id)})
Bulk Inserts插入多个文档
使用insert_many()方法,多个文档以列表的形式插入
#列表new_posts中有两个字典元素,一个字典表示一个文档
>>> new_posts = [{"author": "Mike",
... "text": "Another post!",
... "tags": ["bulk", "insert"],
... "date": datetime.datetime(2009, 11, 12, 11, 14)},
... {"author": "Eliot",
... "title": "MongoDB is fun",
... "text": "and pretty easy too!",
... "date": datetime.datetime(2009, 11, 10, 10, 45)}]
>>> result = posts.insert_many(new_posts)
>>> result.inserted_ids
[ObjectId('...'), ObjectId('...')]
Querying for More Than One Document访问多个文档
find()方法
#无参数输入时,打印出该集合所有文档
>>> for post in posts.find():
... pprint.pprint(post)
...
{u'_id': ObjectId('...'),
u'author': u'Mike',
u'date': datetime.datetime(...),
u'tags': [u'mongodb', u'python', u'pymongo'],
u'text': u'My first blog post!'}
{u'_id': ObjectId('...'),
u'author': u'Mike',
u'date': datetime.datetime(...),
u'tags': [u'bulk', u'insert'],
u'text': u'Another post!'}
{u'_id': ObjectId('...'),
u'author': u'Eliot',
u'date': datetime.datetime(...),
u'text': u'and pretty easy too!',
u'title': u'MongoDB is fun'}
#有参输入时,只打印匹配文档
>>> for post in posts.find({"author": "Mike"}):
... pprint.pprint(post)
...
{u'_id': ObjectId('...'),
u'author': u'Mike',
u'date': datetime.datetime(...),
u'tags': [u'mongodb', u'python', u'pymongo'],
u'text': u'My first blog post!'}
{u'_id': ObjectId('...'),
u'author': u'Mike',
u'date': datetime.datetime(...),
u'tags': [u'bulk', u'insert'],
u'text': u'Another post!'}
Counting文档计数方法
#此方法可计算一个集合所含的文档数
>>> posts.count()
3
#此方法计算集合中满足匹配条件的文档数
>>> posts.find({"author": "Mike"}).count()
2
Range Queries限定访问范围
>>> d = datetime.datetime(2009, 11, 12, 12)
#"$lt"表示限定范围 sort()表示由操作者来分类
>>> for post in posts.find({"date": {"$lt": d}}).sort("author"):
... pprint.pprint(post)
...
{u'_id': ObjectId('...'),
u'author': u'Eliot',
u'date': datetime.datetime(...),
u'text': u'and pretty easy too!',
u'title': u'MongoDB is fun'}
{u'_id': ObjectId('...'),
u'author': u'Mike',
u'date': datetime.datetime(...),
u'tags': [u'bulk', u'insert'],
u'text': u'Another post!'}
Indexing增加索引
#增加索引,并且是独一无二的
>>> result = db.profiles.create_index([('user_id', pymongo.ASCENDING)],
... unique=True)
#显示现有的索引,一个是自动添加的_id,一个是手动增加的索引user_id
>>> sorted(list(db.profiles.index_information()))
[u'_id_', u'user_id_1']
>>> user_profiles = [
... {'user_id': 211, 'name': 'Luke'},
... {'user_id': 212, 'name': 'Ziltoid'}]
>>> result = db.profiles.insert_many(user_profiles)
>>> new_profile = {'user_id': 213, 'name': 'Drew'}
>>> duplicate_profile = {'user_id': 212, 'name': 'Tommy'}
>>> result = db.profiles.insert_one(new_profile) # This is fine.
>>> result = db.profiles.insert_one(duplicate_profile)#有重复user_id则报错
Traceback (most recent call last):
DuplicateKeyError: E11000 duplicate key error index: test_database.profiles.$user_id_1 dup key: { : 212 }