Learn MongoDB 4.x
上QQ阅读APP看书,第一时间看更新

Installing the PyMongo driver package

We do not cover the actual installation of Python itself, as that is beyond the scope of this book. However, we will cover the installation of the PyMongo driver, which sits between Python and MongoDB. Before we dive into the details of installing the PyMongo driver package, it's important to discuss potential driver compatibility issues.

For more information about how to install Python, please consult the documentation at https://docs.python.org/3/using/index.html. For information on the PyMongo driver, see  https://github.com/mongodb/mongo-python-driver.
In many cases, you will find multiple versions of Python installed on the server you plan to use to host MongoDB. If that is the case, you must ensure that the version of PyMongo chosen not only supports the version of MongoDB you are using but the version of Python you plan to use as well!
Python driver compatibility

Before you proceed to install the PyMongo driver, you might need to deal with driver compatibility issues. These issues are twofold, and are detailed as follows:

  • Driver version support for the version of MongoDB you are using
  • Driver version support for the version of Python you are using

There is an excellent documentation reference that cross-references the version of PyMongo against the version of MongoDB. The table can be found here: https://docs.mongodb.com/ecosystem/drivers/pymongo/#mongodb-compatibility. In the table, for example, you will notice that MongoDB version 2.6 is supported by PyMongo driver versions 2.7 all the way up to 3.9. However, do you really want to be using MongoDB version 2? Probably not!

More relevant to this book is the support for MongoDB 4.x. As of the time of writing, the PyMongo driver version 3.9 supports both MongoDB 4.0 and 4.2. On the other hand, if you find that you need a slightly older version of the PyMongo driver, perhaps due to issues with the version of Python you are running (discussed next), you will find that MongoDB 4.0 is supported by PyMongo driver versions 3.7, 3.8, and 3.9.

In the same section of the MongoDB documentation, there is a follow-up table that lists which version of Python supports the various versions of the PyMongo driver. That table can be found here: https://docs.mongodb.com/ecosystem/drivers/pymongo/#language-compatibility.

Many operating systems automatically provide Python; however, more often than not, it's version 2. Interestingly, you will note that the PyMongo driver version 3.9 does in fact support Python version 2.7. If you are using Python version 3, again, you will find that the PyMongo driver version 3.9 supports Python 3.4 through 3.7.

So, in summary, the version of the PyMongo driver that you choose to install is dictated by both the version of MongoDB you plan to use and the version of Python. Next, we'll have a look at a tool called pip.  

pip3

The Python package manager is called pip. You will need pip to install the PyMongo driver, but, just as with Python, there is pip for Python release 2 and also pip3 for Python 3. The pip command does not work with Python 3. If you do not have pip3 available, use the appropriate installation procedure for the operating system that will house the Python code you plan to create, which will communicate with MongoDB.

For further information, please consult the official pip documentation here: https://pip.pypa.io/en/stable/.
Installing PyMongo using pip

There are a number of ways in which you can install the PyMongo driver package, detailed as follows:

  • Compiling the source code
  • Installing a pre-built binary
  • Using pip3

This section describes how to use pip3 to install the PyMongo driver package as well as where PyMongo files are stored in the filesystem. For more information on the latest PyMongo driver, you can consult the Python Package Index (PyPI) (see https://pypi.org/). Enter pymongo in the search dialog box, and a list of pertinent results is displayed. Clicking on the https://pypi.org/project/pymongo/ link, you will see the current version of PyMongo.

To use pip3 to install the PyMongo driver, open a Terminal window, and run the following command. Please note that you will most likely need either root or administrator privileges to perform this command:

pip3 install pymongo

Here is the output from an Ubuntu Linux 20.04 server:

We have now successfully installed PyMongo using pip. The next step is to test the connection.

Testing the connection between Python and MongoDB

For this simple test, all we need is the MongoClient class from the pymongo module. The example shown in this sub-section can be found at /path/to/repo/chapters/02/test_connection.py. Let's have a look at the code, as follows:

  1. We start by defining sample data and importing the needed classes, like this:
data = { 'first_name' : 'Fred', 'last_name'  : 'Flintstone'}
import pprint
from pymongo import MongoClient
  1. Next, we create a MongoClient instance and a reference to the new database test, as follows:
client = MongoClient('localhost')
test_db = client.test

In order to maintain the purity of the test, as we are likely to run it many times, we add a statement that removes all documents from the new collection, test_collection

  1. We then insert and retrieve a single document using the insert_one() and find_one() pymongo collection methods. A typical return value after an insert operation would be the last autogenerated ID,  as shown here:
test_db.test_collection.delete_many({})
insert_result = test_db.test_collection.insert_one(data).inserted_id
find_result = test_db.test_collection.find_one()
  1. Finally, we use pprint to display the results, as follows:
pprint.pprint(insert_result)
pprint.pprint(find_result)

Here is a screenshot of how the output might appear:

The three pymongo collection methods shown— delete_many(), insert_one(), and find_one()—will be described in greater detail in later chapters of this book. For more information, see this web page: https://api.mongodb.com/python/current/api/pymongo/collection.html.

In the next section, you will learn to retrieve and load the source code and sample data provided in the GitHub repository that accompanies this book.