3.6 LIBRARIES
3.7.2.1 Cloudmesh MongoDB Usage Quickstart
3.7.2.3.14 Deleting Documents from a Collection
The deletion of documents with PyMongo is fairly straight forward. To do so, one would use the remove() method of the PyMongo Collection object [22]. Similarly to the reads and updates, specification of documents to be removed is a must. For example, removal of the entire document collection with a score of 1, would required one to use the following command:
The safe parameter set to True ensures the operation was completed [22]. 3.7.2.3.15 Copying a Database
Copying databases within the same mongod instance or between different mongod servers is made possible with the command() method after connecting to the desired mongod instance [27]. For example, to copy the cloudmesh
database and name the new database cloudmesh_copy, one would use the
command() method in the following manner:
There are two ways to copy a database between servers. If a server is not password-prodected, one would not need to pass in the credentials nor to authenticate to the admin database [27]. In that case, to copy a database one would use the following command:
On the other hand, if the server where we are copying the database to is protected, one would use this command instead:
3.7.2.3.16 PyMongo Strengths
cloudmesh.users.remove({"score":1, safe=True})
client.admin.command('copydb', fromdb='cloudmesh', todb='cloudmesh_copy') client.admin.command('copydb', fromdb='cloudmesh', todb='cloudmesh_copy', fromhost='source.example.com')
client = MongoClient('target.example.com', username='administrator', password='pwd') client.admin.command('copydb', fromdb='cloudmesh', todb='cloudmesh_copy', fromhost='source.example.com')
One of PyMongo strengths is that allows document creation and querying natively
“through the use of existing language features such as nested dictionaries and lists” [22].
For moderately experienced Python developers, it is very easy to learn it and quickly feel comfortable with it.
“For these reasons, MongoDB and Python make a powerful combination for rapid, iterative development of horizontally scalable backend applications” [22].
According to [22], MongoDB is very applicable to modern applications, which makes PyMongo equally valuable [22].
3.7.2.4 MongoEngine
“MongoEngine is an Object-Document Mapper, written in Python for
working with MongoDB” [28].
It is actually a library that allows a more advanced communication with MongoDB compared to PyMongo. As MongoEngine is technically considered to be an object-document mapper(ODM), it can also be considered to be
“equivalent to a SQL-based object relational mapper(ORM)” [19].
The primary technique why one would use an ODM includes data conversion
between computer systems that are not compatible with each other [29]. For the purpose of converting data to the appropriate form, a virtual object database
must be created within the utilized programming language [29]. This library is also used to define schemata for documents within MongoDB, which ultimately helps with minimizing coding errors as well defining methods on existing fields [30]. It is also very beneficial to the overall workflow as it tracks changes made to the documents and aids in the document saving process [31].
The installation process for this technology is fairly simple as it is considered to be a library. To install it, one would use the following command [32]:
A bleeding-edge version of MongoEngine can be installed directly from GitHub
by first cloning the repository on the local machine, virtual machine, or cloud. 3.7.2.4.2 Connecting to a database using MongoEngine
Once installed, MongoEngine needs to be connected to an instance of the mongod, similarly to PyMongo [33]. The connect() function must be used to successfully complete this step and the argument that must be used in this function is the name of the desired database [33]. Prior to using this function, the function name needs to be imported from the MongoEngine library.
Similarly to the MongoClient, MongoEngine uses the local host and port 27017 by default, however, the connect() function also allows specifying other hosts and port arguments as well [33].
Other types of connections are also supported (i.e. URI) and they can be completed by providing the URI in the connect() function [33].
3.7.2.4.3 Querying using MongoEngine
To query MongoDB using MongoEngine an objects attribute is used, which is, technically, a part of the document class [34]. This attribute is called the
QuerySetManager which in return
“creates a new QuerySet object on access” [34].
To be able to access individual documents from a database, this object needs to be iterated over. For example, to return/print all students in the
cloudmesh_community object (database), the following command would be
used.
$ pip install mongoengine
from mongoengine import connect connect('cloudmesh_community')
connect('cloudmesh_community', host='196.185.1.62', port=16758)
MongoEngine also has a capability of query filtering which means that a keyword can be used within the called QuerySet object to retrieve specific information [34]. Let us say one would like to iterate over
cloudmesh_community students that are natives of Indiana. To achieve this, one would use the following command:
This library also allows the use of all operators except for the equality operator in its queries, and moreover, has the capability of handling string queries, geo queries, list querying, and querying of the raw PyMongo queries [34].
The string queries are useful in performing text operations in the conditional queries. A query to find a document exactly matching and with state ACTIVE
can be performed in the following manner:
The query to retrieve document data for names that start with a case sensitive AL
can be written as:
To perform an exact same query for the non-key-sensitive AL one would use the following command:
The MongoEngine allows data extraction of geographical locations by using Geo queries. The geo_within operator checks if a geometry is within a polygon.
The list query looks up the documents where the specified fields matches exactly to the given value. To match all pages that have the word coding as an item in the tags list one would use the following query:
print cloudmesh_community.student
indy_students = cloudmesh_community.objects(state='IN')
db.cloudmesh_community.find( State.exact("ACTIVE") )
db.cloudmesh_community.find( Name.startswith("AL") )
db.cloudmesh_community.find( Name.istartswith("AL") )
cloudmesh_community.objects(
point__geo_within=[[[40, 5], [40, 6], [41, 6], [40, 5]]]) cloudmesh_community.objects(
point__geo_within={"type": "Polygon",
"coordinates": [[[40, 5], [40, 6], [41, 6], [40, 5]]]})
class Page(Document):
tags = ListField(StringField())
Overall, it would be safe to say that MongoEngine has good compatibility with Python. It provides different functions to utilize Python easily with MongoDBand which makes this pair even more attractive to application developers.
3.7.2.5 Flask-PyMongo
“Flask is a micro-web framework written in Python” [35].
It was developed after Django, and it is very pythonic in nature which implies that it is explicitly the targeting the Python user community. It is lightweight as it does not require additional tools or libraries and hence is classified as a Micro- Web framework. It is often used with MongoDB using PyMongo connector, and it treats data within MongoDB as searchable Python dictionaries. The applications such as Pinterest, LinkedIn, and the community web page for Flask are using the Flask framework. Moreover, it supports various features such as the RESTful request dispatching, secure cookies, Google app engine compatibility, and integrated support for unit testing, etc [35]. When it comes to connecting to a database, the connection details for MongoDB can be passed as a variable or configured in PyMongo constructor with additional arguments such as username and password, if required. It is important that versions of both Flask and MongoDB are compatible with each other to avoid functionality breaks [36]. 3.7.2.5.1 Installation
Flask-PyMongo can be installed with an easy command such as this:
PyMongo can be added in the following manner: