MongoDBClientAccess
NDS Labs Project: QMCDB
Accessing the MongoDB Server
The MongoDB server runs on a machine in Labs' nebula cluster. It can be accessed remotely with generic MongoDB clients, including the UNIX command-line tool, mongo, and the python package, PyMongo.
Installing the MongoDB clients
MongoDB and the mongo command-line client
When installing the mongo client, it's easiest to just install the whole MongoDB package. It's important to get the latest version (>3.0.3) or you will likely have trouble connecting to the server. To install, follow the instructions for your local platform found here; it's pretty easy, generally. Note that it's best to follow these instructions rather than simply taking the default available through your OS's normal package distribution as the latter typically is not recent enough.
PyMongo
If you want to interact with the database via python, install a recent version of PyMongo; use the pip install
instructions given here. Using pip
ensures you can get the latest version (unlike getting it via your OS distribution).
Note that with Linux, pip is not typically installed by default. This you can install via the distribution's package system (e.g. yum
or apt-get
).
Connecting to the Database with mongo
The MongoDB site provides a nice Getting Started guide. It goes over how to add data and do a simple search.
Our MongoDB instance is accessible via the standard MongoDB port on our Labs host machine. Detailed host and login information has been distributed by email. I've started us with two databases to get us started qmcdb
and qmcdb_dev
, but you can create others (with the right account). qmcdb_dev
is the preferred database for "playing around".
To connect, try:
mongo --host HOST-ADDRESS qmcdb_dev --username ACCOUNT-NAME --password
(Substitute in the actual host and username for HOST-ADDRESS adn ACCOUNT-NAME.) You will be prompted for your password, and then it will drop you into an interactive shell. To exit this shell, type Ctrl-D
or exit
.
For the moment, the regular accounts are tied to the qmcdb_dev
database. If you want to attach to the qmcdb
or any other, you need to use the --authenticationDatabase
option:
mongo --host HOST-ADDRESS --authenticationDatabase qmcdb_dev DATABASE --username ACCOUNT-NAME --password
where DATABASE is the name of the database to connect to, such as qmcdb
. Alternatively, if you are already logged into the mongo
shell, you can type use qmcdb
to switch to the qmcdb
database. More details about the command line can be found in the mongo command man page.
To create a new database, you'll need to the manager account (see email for details). Note that with MongoDB, a database does not get "created" until you actually write something to it.
Tips on Using the Command-line Interface
- The Getting Started guide talks about adding documents to collections; these terms are analogous to rows and tables in traditional SQL databases.
- You can load JSON documents/records into a collection either in the shell with the
insert
command (e.g.db.executions.insert(
json)
; example) or outside the shell with themongoimport
command (example). If you use the latter, you will need to use the same command-line options as noted above to provide the host, username and password.
- Use the find command to get records out of the database. The Getting Started guide gives an introduction to its use; full details can be in the page on Query Documents.