-
Notifications
You must be signed in to change notification settings - Fork 56
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.
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
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
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
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.
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.
...
>
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}
The information provided in this article is accurate at the time of writing, but on-going development in the open-source projects involved may make the information incorrect or obsolete. Please open issue or contact us on IBM Z Community if you have any questions or feedback.