Skip to content

Building Python MongoDB Driver

aborkar-ibm edited this page May 7, 2019 · 24 revisions

Building Python MongoDB Driver

Below versions of Python MongoDB Driver are available in respective distributions at the time of creation of these build instructions:

  • Ubuntu 16.04 has 3.2
  • Ubuntu 18.04 has 3.6.1
  • Ubuntu 19.04 has 3.7.1

The instructions provided below specify the steps to build Python MongoDB Driver (PyMongo) version 3.8.0 on Linux on IBM Z for the following distributions:

  • RHEL (6.10, 7.4, 7.5, 7.6)
  • SLES (12 SP4, 15)
  • Ubuntu (16.04, 18.04, 19.04)

General Notes:

  • When following the steps below please use a standard permission user unless otherwise specified.
  • A directory /<source_root>/ will be referred to in these instructions, this is a temporary writable directory anywhere you'd like to place it.

Step 1: Building and Installing PyMongo

1.1) Install dependencies

export SOURCE_ROOT=/<source_root>/
  • RHEL 6.10

    sudo yum install -y gcc git
    • Install Python 2.7.x -- Instructions for building Python can be found here.

    Note: You may need to use sudo env PATH=$PATH python /<command>/ in order to use Python 2.7.x on RHEL 6.10.

  • RHEL (7.4, 7.5, 7.6)

    sudo yum install -y gcc git python-devel
  • SLES (12 SP4, 15)

    sudo zypper install -y gcc git python-devel
  • Ubuntu (16.04, 18.04, 19.04)

    sudo apt-get update
    sudo apt-get install -y gcc git python-dev

1.2) Download PyMongo source and Install

cd $SOURCE_ROOT
git clone git://github.com/mongodb/mongo-python-driver.git pymongo
cd pymongo
git checkout 3.8.0
sudo python setup.py install

1.4) Execute test cases

Access to MongoDB Server is required to execute test cases.

  • If MongoDB Server is running on same machine, then execute the following:

    sudo python setup.py test
  • If MongoDB Server is running on remote machine, then execute the following:

    export DB_IP= <MongoDB_Server_IP>
    sudo -E python setup.py test

Step 2: Basic validation test

The example code section given below is used to perform a basic test to ensure that the MongoDB Ruby Driver is working as expected, and can connect to, modify and query a MongoDB server. Instructions to install MongoDB can be found on their official website here.

2.1) Prerequisites

To run this test, MongoDB must be running on the default port, 27017. The following commands are an example of how to start a MongoDB server and then connect to it with the client shell.

Issue the following command to start mongod and to check if it is up, connect to it with the MongoDB shell.

mongod > /tmp/mongodb.log &
mongo --host localhost --port 27017
MongoDB shell version v4.0.9
connecting to: mongodb://localhost:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e5975020-2ebc-4576-800e-55ec3b780ba1") }
MongoDB server version: 4.0.9
Welcome to the MongoDB shell.
...
>

2.2) The Test Code

Create a file named test.py with the content shown below. If you are connecting to a remote server then you need to substitute the localhost with the hostname or IP address of the MongoDB server.

import pprint
import pymongo

from pymongo import MongoClient

server="localhost";
database="ibm_test_db";
collection="mongodb_python_driver";

serverdb="mongodb://" + server + ":27017/";

client = MongoClient(serverdb)
db = client[database];
db[collection].drop();

header = {"company": "IBM",
    "project": "MongoDB Driver",
    "language": "python",
    "version": "3.8.0"};

db[collection].insert_one(header);

for i in range (0, 3):
    doc = {"line": i};
    db[collection].insert_one (doc);

for gotdoc in db[collection].find():
    pprint.pprint(gotdoc);

Execute the test program by:

python test.py

Executing the test program should produce a similar output to this (the Object IDs will vary):

{u'_id': ObjectId('5cc9de821f1f1182e662bbae'),
 u'company': u'IBM',
 u'language': u'python',
 u'project': u'MongoDB Driver',
 u'version': u'3.8.0'}
{u'_id': ObjectId('5cc9de821f1f1182e662bbaf'), u'line': 0}
{u'_id': ObjectId('5cc9de821f1f1182e662bbb0'), u'line': 1}
{u'_id': ObjectId('5cc9de821f1f1182e662bbb1'), u'line': 2}

References:

Clone this wiki locally