MongoDB usage tips: connecting via a mongo container image๐Ÿ”—

Pulling a container with MongoDB๐Ÿ”—

In the examples below, we'll be working with MongoDB version 5.0.2 via a Singularity container. First, pull a mongoDB container from the Docker repository via

singularity pull mongo.img docker://mongo

This will create the file mongo.img in your working directory. You can see the version of mongoDB living in this container via

janeh@oslic5:~$ singularity exec mongo.img mongo --version
MongoDB shell version v5.0.2
Build Info: {
    "version": "5.0.2",
    "gitVersion": "6d9ec525e78465dcecadcff99cce953d380fedc8",
    "openSSLVersion": "OpenSSL 1.1.1f  31 Mar 2020",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"

Since we'll be working with a container to use MongoDB binaries, you may want to refer to our docs on containers.

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 can create a file called .mongoshrc.js that will live in your home directory ($HOME/.mongoshrc.js) and have the following format:

db = connect("mongodb://<host>:<port>/<database_name>?tls=true&tlsAllowInvalidCertificates=true", "<database_user>", "<database_password>");

(Be sure to replace all fields enclosed in <> with your info.) For example, I might have the following in my .mongoshrc.js file:

db = connect("mongodb://", "userFXE", "mypassword");

Once this file is in place in $HOME, you should be able to connect to your mongoDB database via a command of the form

singularity exec <path to your mongo image> mongosh --host <host>:<port> --tls --tlsAllowInvalidCertificates

as in

janeh@oslic5:~$ singularity exec mongo.img mongosh --host --tls --tlsAllowInvalidCertificates
Current Mongosh Log ID: 615251d615a247ec598e7b0d
Connecting to:      mongodb://
Using MongoDB:      undefined
Using Mongosh:      1.0.5

For mongosh info see:


Note that the username, password, and database name are all passed when mongosh automatically reads ~/.mongoshrc.js.

More complex workflows: multiple databases๐Ÿ”—

If you will be connecting to multiple databases, you may not wish to rely on ~/.mongoshrc.js as this will connect you to the same database every time. Instead, you can create different .js files with the same file format specified for .mongoshrc.js above for each of your databases. You will then specify which .js should be read on the command line. If your database's credentials are stored in database1.js, for example, you can connect via

singularity exec mongo.img mongosh --host <host>:<port>  --tls --tlsAllowInvalidCertificates -f ~/database1.js --shell

as in

janeh@oslic5:~$ singularity exec mongo.img mongosh --host  --tls --tlsAllowInvalidCertificates -f database1.js --shell
Current Mongosh Log ID: 6152552182a3df457b417e30
Connecting to:      mongodb://
Using MongoDB:      undefined
Using Mongosh:      1.0.5

For mongosh info see:

Loading file: database1.js

Note that the command line options -f <.js filename> and --shell have been added to the command used to connect when we simply relied on ~/.mongoshrc.js.

How to back up a mongoDB database๐Ÿ”—

You can use mongodump to back up a mongoDB database. First, create a configuration file (in your $HOME) storing your database password and connection string with the following format

password: <password>
uri: mongodb://<username>@<host>:<port>/<database_name?ssl=true&tlsInsecure=true>

as in

password: mypassword
uri: mongodb://

If the above is stored in a file called mongodump.conf, you can run mongodump via

singularity exec mongo.img mongodump --out <output directory> --config=mongodump.conf

as in

janeh@oslic5:~$ singularity exec mongo.img mongodump --out /g/g0/janeh/mongodump_output --config=mongodump.conf
2021-09-27T17:07:19.551-0700    writing sampledb.collection to /g/g0/janeh/mongodump_output/sampledb/collection.bson
2021-09-27T17:07:19.553-0700    done dumping sampledb.collection (2 documents)

Here, the <output directory> called mongodump_output was created with a subdirectory sampledb, named for the sampledb database. Information about my database is written in that directory's files.

See the full docs for more on using mongodump.

How to restore a database

You can restore a database via mongorestore. To do so, you'll need to specify an input directory with --dir= that stores the output of a mongodump. You also need to specify a configuration file with the same format as the one needed for a mongodump. The general syntax are

singularity exec mongo.img mongorestore --dir=<input dir> --config=<config file>

and to restore the database from the mongodump I performed in the section above, I might run

singularity exec mongo.img mongorestore --dir=/g/g0/janeh/mongodump_output --config=mongodump.conf

See the full docs for more on using mongorestore.