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/mongodb.info.
Using the credentials in your mongodb.info, you'll want to assemble a connection string of the format:
"mongodb://<database_user>:<password>@<host>:<port>/<database_name>?tls=true&tlsAllowInvalidCertificates=true"
For example, mine is "mongodb://userFXE:mypassword@any.apps.czapps.llnl.gov:32050/sampledb?tls=true&tlsAllowInvalidCertificates=true"
You might choose to open a python interpreter and run the following lines, after replacing any information in <> with the info from your mongodb.info 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", "any.apps.czapps.llnl.gov", "32050", DB_NAME) >>> DB_URI 'mongodb://userFXE:mypassword@any.apps.czapps.llnl.gov:32050/sampledb?ssl=true&tlsAllowInvalidCertificates=true'
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=['any.apps.czapps.llnl.gov:32050'], document_class=dict, tz_aware=False, connect=True, ssl=True, ssl_cert_reqs=<VerifyMode.CERT_NONE: 0>) >>> db Database(MongoClient(host=['any.apps.czapps.llnl.gov:32050'], 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 https://pymongo.readthedocs.io/en/stable/tutorial.html 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 .