-
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:
- RHEL (8.1, 8.2) has
3.6.1
- RHEL (7.6, 7.7, 7.8) has
3.5.1
- Ubuntu 16.04 has
3.2
- Ubuntu 18.04 has
3.6.1
- Ubuntu 20.04 has
3.10.1
The instructions provided below specify the steps to build Python MongoDB Driver (PyMongo) version 3.10.1 on Linux on IBM Z for the following distributions:
- RHEL (7.6, 7.7, 7.8, 8.1, 8.2)
- SLES (12 SP4, 12 SP5, 15 SP1)
- Ubuntu (16.04, 18.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 (7.6, 7.7, 7.8, 8.1, 8.2)
sudo yum install -y python3-devel python3-setuptools wget curl diffutils
-
SLES (12 SP4, 12 SP5, 15 SP1)
sudo zypper install -y python3 python3-pip wget curl
-
Ubuntu (16.04, 18.04)
sudo apt-get update sudo apt-get install -y python3-dev python3-pip wget curl python3-setuptools
sudo pip3 install pymongo==3.10.1
The example code section given below is used to perform a basic test to ensure that the MongoDB Python 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.10.1"};
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:
python3 test.py
Executing the test program should produce a similar output to this (the Object IDs will vary):
{'_id': ObjectId('5da9a16bd344339bfb40513f'),
'company': 'IBM',
'language': 'python',
'project': 'MongoDB Driver',
'version': '3.10.1'}
{'_id': ObjectId('5da9a16bd344339bfb405140'), 'line': 0}
{'_id': ObjectId('5da9a16bd344339bfb405141'), 'line': 1}
{'_id': ObjectId('5da9a16bd344339bfb405142'), '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.