首先,必须确认如下环境已经安装:python
1. Pythonweb
2. MongoDBmongodb
3. pymongo数据库
import pymongo
from pymongo import MongoClient client = MongoClient(host, port)
引用MongoClient来建立数据库链接实例。在MongoDB中,默认的host和port以下:post
client = MongoClient('localhost', 27017)
或者使用MongoDB的URL来引用:spa
client = MongoClient('mongodb://localhost:27017/')
任何一个链接实例均可以链接一个或者多个独立的数据库。这里默认已有一个名为test_db的数据库,下面是链接方法:code
database = client.test_database
或者当你的数据库名称不符合Python标准的时候,能够用:orm
database = client['test-database']
Collection在这里的意思是一个存储在MongoDB中的文件集合,至关于关系型数据库中的table。具体方法和database同样:blog
collection = db.test_collection
或者:排序
collection = db['test-collection']
数据在MongoDB中是以JSON的方式储存的。在pymongo中使用字典的形式来保存数据。事例以下:
>>> import datetime >>> post = {"author": "Mike", ... "text": "My first blog post!", ... "tags": ["mongodb", "python", "pymongo"], ... "date": datetime.datetime.utcnow()}
咱们使用insert_one()方法来插入一条数据
>>> posts = db.posts >>> post_id = posts.insert_one(post).inserted_id >>> post_id ObjectId('...')
若是数据不含有“_ID”,那么当它被插入到数据库中的时候,数据库就会自动赋予它一个“_ID”。在整个MongoDB中,这个_ID都是惟一的。
当post这个数据被插入的时候,它也就在MongoDB中同时被建立了一个Collection。咱们能够用以下的方法来验证这些Collection:
>>> db.collection_names(include_system_collections=False) [u'posts']
在MongoDB中最为基础的查询语言就是find_one()。这种方法只能返回一条查询结果,当有多个查询结果符合查询条件的时候,数据库会返回第一条。
>>> import pprint >>> pprint.pprint(posts.find_one()) {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!'}
返回的结果也是以字典的方式呈现的。
一样地,这个方法也支撑具体的条件查询,例如,咱们想要得到做者为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!'}
若是咱们试着查询另外一个不存在的做者,例如Eliot,返回的结果就是空:
>>> posts.find_one({"author": "Eliot"}) >>>
因为_ID是惟一的,当咱们知道这个ID的时候,咱们能够经过这个ID进行查询
>>> post_id 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!'}
注意:上例中的ObjectID的数据类型并非str
>>> post_id_as_str = str(post_id) >>> posts.find_one({"_id": post_id_as_str}) # No result >>>
在网页应用中,最多见的就是从request URL中或者ID并查询,此时要注意的便是这个ID的数据类型问题了。
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)})
可使用insert_many()来插入多条数据。使用这种插入方法,并不须要多条命令。
>>> 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('...')]
注意:在第二条数据中,加入了一个与第一条数据格式不符合的数据点“title”,而数据库不会发生错误,这也就是MongoDB的优势之一:不会也别局限于数据点的格式。
可使用find()方法来查询多条数据,返回的是一个Cursor实例,咱们能够遍历全部匹配的数据。
>>> 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'}
一样地,find()一样支持条件查询:
>>> 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!'}
当咱们只想知道有多少数据知足个人查询条件的时候,可使用count()来对查询结果计数。
>>> posts.count()
3
>>> posts.find({"author": "Mike"}).count() 2
MongoDB一样支持不少的高级查询的功能,例如,咱们在下面的查询中限定日期,并对查询结果根据做者author进行排序:
>>> d = datetime.datetime(2009, 11, 12, 12) >>> 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!'}
索引
加入索引系统能够加速查询的进程而且添加更多的查询功能。在这个例子中,咱们将要演示索引的建立以及使用:
首先,先建立一个索引
>>> result = db.profiles.create_index([('user_id', pymongo.ASCENDING)], ... unique=True) >>> sorted(list(db.profiles.index_information())) [u'_id_', u'user_id_1']
在返回的结果中,有两个ID,一个是MongoDB自动建立的,另外一个是咱们新加上去的。此时,咱们设置一些用户ID:
>>> user_profiles = [ ... {'user_id': 211, 'name': 'Luke'}, ... {'user_id': 212, 'name': 'Ziltoid'}] >>> result = db.profiles.insert_many(user_profiles)
然而索引系统就会自动地阻止咱们设置在Collection中重复的ID:
>>> 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) Traceback (most recent call last): DuplicateKeyError: E11000 duplicate key error index: test_database.profiles.$user_id_1 dup key: { : 212 }