MongoDB is an open-source document-oriented database management system (DBMS) with flexible schemas. MongoDB was founded by Dwight Merriman and Eliot Horowitz who faced with development issues and struggled with relational database management system while building web applications.
According to one of its founders, the MongoDB name comes from the word humongous and simply means that the database can handle lots of data. Merriman and Horowitz helped to create the 10gen Inc. in 2007 with an aim to commercialise database program and related software. The company was renamed to MongoDB Inc. in 2013.
An open-source database was released in 2009. Now it is available under the Server Side Public License (SSPL) license terms.
The document itself is a set of key/value pairs. Documents have a dynamic schema. Dynamic schemas mean that documents from one collection do not necessarily have the same set of fields and structures. It also means that the common fields in the documents collections can contain different types of data. Below is an example of a document structure:
{
_id: ObjectId('7df78ad8902c')
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,
comments: [
{
user: 'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},
{
user: 'user2',
message: 'My second comments',
dateCreated: new Date(2011,1,25,7,45),
like: 5
}
]
}
_id is the 12-byte hexadecimal number which assures the uniqueness of each document. You obviously can insert it in the document. If it’s not specified, MongoDB provides a unique ID for each document.
DBMS uses JSON style documents that are stored in the BSON binary format. Thanks to the GridFS protocol, MongoDB has the capability to store and retrieve files. Like the other document-oriented DBMS (CouchDB, etc.), MongoDB is not a relational DBMS. There is no such thing as a "transaction." Atomicity is only guaranteed on a whole document-level, so the partial update of the document can not happen. Also, there is no an "isolation" concept: any data that is read by one client can simultaneously be changed by another client.
The following are some of the MongoDB advantages:
MongoDB | Relational DBMS | Key-Value | |
Rich data model | Yes | No | No |
Dynamic schema | Yes | No | Yes |
Data validation | Yes | Yes | No |
Typed data | Yes | Yes | No |
Localization of data | Yes | No | Yes |
Fields updating | Yes | Yes | No |
So, how to use MongoDB in Python? Take a look at our short Python MongoDB tutorial.
PyMongo is a tool for working with MongoDB and is the official recommended way to work when using Mongo database in Python.
In PyMongo we use dictionaries to represent documents. As an example, the following dictionary might be used to represent a blog post:
import datetime
post = {
"author": "Mike",
"text": "My first blog post!",
"tags": ["mongodb", "python", "pymongo"],
"date": datetime.datetime.utcnow()
}
Documents can contain native Python types (such as datetime.datetime) which will be automatically converted to and from the appropriate BSON types.
To add a document into the collection, we can use the insert_one() method:
posts = db.posts
post_id = posts.insert_one(post).inserted_id
The most useful type of query is the find_one method. This method returns a single document matching a query (or none, in case if there are no matches). Here we use find_one() to get the first document from the posts collection:
>>> posts.find_one()
The result is a document that we inserted previously. find_one() also supports querying on specific elements that the document must match.
To get more documents, we use the find() method. find() returns a Cursor instance, which allows us to iterate over all matching documents.
Also, we can limit the find() returned results. We only get documents with author “Mike” here:
for post in posts.find({"author": "Mike"}):
print(post)
At the PyMongo core is the MongoClient object, which is used to make connections and queries to a MongoDB database cluster. It can be used to connect to a standalone mongod instance, a replica set or mongos instances. Repository: https://github.com/mongodb/mongo-python-driver
Documentation: https://api.mongodb.com/python/current/
MongoEngine is ODM (Python MongoDB ORM, but for document-oriented database) that allows to work with MongoDB on Python. It uses simple declarative API similar to Django ORM.
To specify the Python MongoDB schema document, we create a class that is inherited from the Document base class. Fields are determined from adding the document’s class attributes.
from mongoengine import *
class Metadata(EmbeddedDocument):
tags = ListField(StringField())
revisions = ListField(IntField())
class WikiPage(Document):
title = StringField(required=True)
text = StringField()
metadata = EmbeddedDocumentField(Metadata)
Now, when we have identified how our documents will be structured, we begin adding some of the documents to the database. Firstly, we need to create an object:
page1 = WikiPage(title='Example 1', text='Wiki Page 1').save()
We could also define our object using the attribute syntax:
page2 = WikiPage(title='Example 2')
page2.text = 'Wiki Page 2'
page2.metadata = Metadata(tags=['tag 1', 'tag 2']).save()
page2.save()
Each document class (i.e. any class that is directly or indirectly inherited from the document) has an attribute and objects that are used to access the documents in the collection of database associated with this class. So, let's see how to get the headlines of our pages:
for page in WikiPage.objects:
print(page.title)
Here is a brief list of some of the main features of MongoEngine:
Repository: https://github.com/MongoEngine/mongoengine
Documentation: http://docs.mongoengine.org/
Motor is an async driver for MongoDB. It can be used as Tornado or asyncio-applications. Motor never blocks the event loop when connecting to MongoDB or when performing input/output operations. This driver is practically a wrapper over the entire API PyMongo for non-blocking access to MongoDB.
The tornado.gen module allows using subprogrammes to simplify an asynchronous code. It supports Tornado-applications with multiple IOLoops. It can transfer data from GridFS to Tornado RequestHandler, using stream_to_handler () or the GridFSHandler class.
Motor provides a single class-client - MotorClient. Unlike MongoClient PyMongo, MotorClient does not actually connect in the background on startup. Instead, it is connected on demand, at the first operation request.
Motor supports almost every PyMongo method, but the methods take an additional callback function.
Motor uses gevent-like method to wrap PyMongo and run its asynchronously, presenting a classic callback interface to Tronado applications. This driver can easily keep up with the PyMongo development in the future.
Repository: https://github.com/mongodb/motor
Documentation: https://motor.readthedocs.io/en/
MotorEngine is created based on Motor. Motorengine is a port of MongoEngine.
MongoKit is a Python-module, a PyMongo wrapper, that brings structured schemes and screening layer.
MongoKit uses simple types of the Python data to describe the structure of the document. MongoKit is pretty fast and has access to clean PyMongo-layer without API changes to increase the speed. It has lots of additional features, such as automatic reference to a document, user types or i18n support. Documents are improved by the validate() Python-dictionaries method.
Document declaration is as follows:
>>> from mongokit import
>>> import datetime
>>> connection = Connection()
>>> @connection.register
... class BlogPost(Document):
... structure = {
... 'title':unicode,
... 'body':unicode,
... 'author':unicode,
... 'date_creation':datetime.datetime,
... 'rank':int
... }
... required_fields = ['title','author', 'date_creation']
... default_values = {'rank':0, 'date_creation':datetime.datetime.utcnow}
Setting the link and registering our objects:
>>> blogpost = con.test.example.BlogPost() # this uses the database "test" and the collection "example"
>>> blogpost['title'] = u'my title'
>>> blogpost['body'] = u'a body'
>>> blogpost['author'] = u'me'
>>> blogpost
{'body': u'a body', 'title': u'my title', 'date_creation': datetime.datetime(...), 'rank': 0, 'author': u'me'}
>>> blogpost.save()
Saving an object will call the validate() method. A more complex structure can be used as follows:
>>> @connection.register
... class ComplexDoc(Document):
... database = 'test'
... collection = 'example'
... structure = {
... "foo" : {"content":int},
... "bar" : {
... 'bla':{'spam':int}
... }
... }
... required_fields = ['foo.content', 'bar.bla.spam']
Repository: https://github.com/namlook/mongokit/
Documentation: http://namlook.github.io/mongokit/
Hope you managed to clear up for yourself how to use MongoDB with Python. When you select a Python driver for MongoDB, you should answer two questions:
In the case of asynchronous applications - you need Motor or MotorEngine.
All synchronous drivers are wrappers for PyMongo. If you do not need to fix the structure of the document, the easiest way for you is to work directly with PyMongo.
If you want to fix the structure of the documents in the code, you can take MongoEngine or MongoKit. Mainly, we are working with Django, so it is more natural for us to us MongoEngine.
Also you can contact us if you want to learn more about Python MongoDB best practices.
Read more about MVP development services and web app development.