6.1 Python

The data stored in MongoDB can be accessed using the Python programming language, and in both Ubuntu and Windows, the Anaconda Distribution Python was used for this purpose.

In addition to Python, you should install the PyMongo package, which contains the tools to work with MongoDB. In Ubuntu the installation of PyMongo was done using the command:

$ conda install pymongo

and in Windows 10 using the command:

C:\Users\Halley\Anaconda3\Scripts> .\conda.exe install pymongo

To test the access to MongoDB using Python the following script was written:

# In MongoDB, searches the 'pages' collection of
# 'reficio' database for a document with title
# "Sébastien Auguste Sisson", and shows its excerpt.
import pymongo
import pprint
from pymongo import MongoClient
conn = MongoClient('localhost', 27017)
db = conn.reficio
pages = db.pages
pprint.pprint(pages.find_one(
    { "title" : "Sébastien Auguste Sisson" },
    { "title" : 1, "excerpt" : 1 } )
)

The execution of this script in the Python shell is shown below:

$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> # In MongoDB, searches the 'pages' collection of
... # 'reficio' database for a document with title
... # "Sébastien Auguste Sisson", and shows its excerpt.
... import pymongo
>>> import pprint
>>> from pymongo import MongoClient
>>> conn = MongoClient('localhost', 27017)
>>> db = conn.reficio
>>> pages = db.pages
>>> pprint.pprint(pages.find_one(
...     { "title" : "Sébastien Auguste Sisson" },
...     { "title" : 1, "excerpt" : 1 } )
... )
{'_id': ObjectId('5afc06e526b24ab3365074a7'),
 'excerpt': 'Sébastien Auguste Sisson, conhecido no também como Sebastião '
            'Augusto Sisson e S. A. Sisson, (1824 – 1893) foi um '
            'litógrafo, desenhista e biógrafo francês radicado no Brasil. Leia '
            'mais na Wikipédia. Livro',
 'title': 'Sébastien Auguste Sisson'}
>>>

The featured image shows the debugging of the script in Visual Studio Code on Ubuntu.

6. Programming Languages