MongoDB ======= Connecting to MongoDB from the command line ------------------------------------------- The `MongoDB shell `_ is the standard tool for connecting to MongoDB databases from the command line. To install the MongoDB client on a Faculty server, run: .. code-block:: bash $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6 $ echo "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list $ sudo apt-get update $ sudo apt install mongodb-org-shell This installs version 3.6 of the MongoDB client. This version of the client will not work with servers running MongoDB version 2.6 or earlier. To install a different version of the MongoDB client, refer to the `MongoDB documentation `_ for that version, following the instructions for Ubuntu 16.04. You only need to install `mongodb-org-shell`, rather than the entire `mongodb-org` package. To connect to a MongoDB database, you need to know: - its `hostname`: this is usually a string like ``customers.mydomain.com``; - its `port`: the default port for MongoDB is 27017; - the name of the database that you want to connect to; - your username and password for the database server (note that this is different to your Faculty username). If you are unsure of these, you should ask your database administrator. Once you have the MongoDB client installed, connect to the database with: .. code-block:: bash $ mongo --host HOSTNAME This will open a MongoDB shell, which you can use to authenticate: .. code-block:: javascript // switch to the admin database for your MongoDB server // (this is probably 'admin', but if not, you should talk to your server administrator) > use admin > db.auth('USERNAME', 'PASSWORD'); // log in > use my_org_db // switch to the database you want to use > db.customers.count(); // explore your database Connecting to MongoDB from Python --------------------------------- The official Python package for interacting with MongoDB databases is `PyMongo `_. Install it on a Faculty server with: .. code-block:: bash $ pip install pymongo You can then connect to a MongoDB database with: .. code-block:: python from pymongo import MongoClient client = MongoClient( 'HOSTNAME', username='USERNAME', password='PASSWORD' ) # You can now connect to your database and do some data science db = client.get_database('my_org_db') db['customers'].count() client.close() # Close the connection to free up space on the server .. note:: We close the connection to allow the database server to reclaim resources. This can be critical in a Jupyter notebook, since the kernel remains alive for a long time. .. note:: We recommend avoiding pasting database passwords and other connection details in many notebooks in a project. Have a look at :ref:`package_connection_details` for a recommended strategy for managing database connection details.