MongoDB usage tips: connecting via pymongođź”—

Installing pymongođź”—

To install pymongo, you'll first need to create a python virtual environment (See docs.):

/usr/tce/packages/python/python-3.8.2/bin/virtualenv --system-site-packages mongoenv

This will create a subdirectory in your working directory called mongoenv . Run

source mongoenv/bin/activate

to activate this virtual environment. Note that (mongoenv) will show up before your command line prompt, as in:

janeh@oslic6:~$ source mongoenv/bin/activate
(mongoenv) janeh@oslic6:~$ 

You can now install pymongo with

pip install pymongo

After the installation is complete, note that you can open an interpreter and import pymongo without errors:

(mongoenv) janeh@oslic6:~$ python
Python 3.8.2 (default, Mar 18 2020, 12:19:58)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo

How to connect to MongoDBđź”—

Once your MongoDB account and server have been provisioned, you will find credentials you can use to connect to mongodb under /usr/workspace/<lcusername>/.lciaas/. This directory should have a subdirectory <zone>-<lcusername> (cz-janeh or rz-janeh for me on the CZ or RZ, respectively) in which you'll find the file mongodb/

Using the credentials in your, you'll want to assemble a connection string of the format:


For example, mine is "mongodb://"

You might choose to open a python interpreter and run the following lines, after replacing any information in <> with the info from your file on workspace:

DB_NAME = "<database_name>"
DB_URI = "mongodb://%s:%s@%s:%s/%s?ssl=true&tlsAllowInvalidCertificates=true" %("<database_user>", "<password>", "<host>", "<port>", DB_NAME)

In my case, this will look like

(mongoenv) janeh@oslic2:~$ python
Python 3.8.2 (default, Mar 18 2020, 12:19:58)
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> DB_NAME = "sampledb"
>>> DB_URI = "mongodb://%s:%s@%s:%s/%s?ssl=true&tlsAllowInvalidCertificates=true" %("userFXE", "mypassword", "", "32050", DB_NAME)
>>> DB_URI

Next, run from pymongo import MongoClient and connect to mongoDB with client = MongoClient(DB_URI). You can access your database with db = client[DB_NAME]:

>>> from pymongo import MongoClient
>>> client = MongoClient(DB_URI)
>>> db = client[DB_NAME]
>>> client
MongoClient(host=[''], document_class=dict, tz_aware=False, connect=True, ssl=True, ssl_cert_reqs=<VerifyMode.CERT_NONE: 0>)
>>> db
Database(MongoClient(host=[''], document_class=dict, tz_aware=False, connect=True, ssl=True, ssl_cert_reqs=<VerifyMode.CERT_NONE: 0>), 'sampledb')

From here, you can modify and query your database, as below. See for full pymongo documentation.

>>> mycol = db["CZmachines"]
>>> mydict = {"Quartz" : "TOSS", "Ruby": "TOSS", "Lassen": "RHEL", "Ray": "RHEL"}
>>> mycol.insert_one(mydict)
<pymongo.results.InsertOneResult object at 0x2aaab0eeba80>
>>> for x in client.sampledb["CZmachines"].find():
...     print(x)
{'_id': ObjectId('615353766419c3c70f2aa694'), 'Quartz': 'TOSS', 'Ruby': 'TOSS', 'Lassen': 'RHEL', 'Ray': 'RHEL'}

Backing up and restoring mongoDB databasesđź”—

We recommend using mongodump and mongorestore to back up and restore your mongoDB databases. We provide instructions on using these commands in MongoDB Containers .