Setting up MongoDB

From Knowledge Kitchen
Jump to navigation Jump to search


Create a MongoDB server account

In order to use MongoDB, you will need to initialize your database on the server. You only need to do this once.

Follow the following steps to set up your MongoDB account

  1. visit our department's MongoDB page and click the link to the MongoDB Manager:
  2. log in with your CIMS username and password
  3. you now have a MongoDB account up and running with the following credentials:
    • Your MongoDB database name is the same as your CIMS username - make a note of this
    • Your MongoDB username is the same as your CIMS username - make a note of this
    • Your MongoDB password will be displayed on this page - make a note of this

Kick the tires

The following instructions will allow you to connect the running MongoDB server and try out the MongoDB client.

Log into the i6 web server

Follow the instructions for remotely logging into the i6 web server

You should now be logged into your account on the i6.cims.nyu.edu web server.

Load the MongoDB environment module

At the i6 command prompt, run the following command to auto-configure your UNIX shell environment so that it can properly run MongoDB.

  • This needs to be done only once for every i6 session.
module load mongodb-3.6

Run the MongoDB client

Assuming you have already loaded the MongoDB environment, at the i6 command prompt, run the following command to enter the mongo client:

mongo xxxxxx --host class-mongodb.cims.nyu.edu -u xxxxxx -p
<enter your MongoDB password when prompted>

Where xxxxxx is your MongoDB username (same as your CIMS/i6 username).

You should now be running the MongoDB shell, which is connected to the class-mongodb.cims.nyu.edu database server.

Try issuing commands

Javascript

The MongoDB includes a complete Javascript interpreter. You can issue any Javascript command:

2+2

MongoDB should respond with "4".

The MongoDB Javascript API includes a large number of built-in Javascript functions to do most database operations. You can set up, manage, and perform CRUD queries on your database by issuing these Javascript function calls.

Additional commands

Besides pure Javascript, the MongoDB shell understands additional commands that experienced database users might expect to find. For example:

Show a list of databases

//note: on our class-mongodb server, the following command does not work... ignore it
show dbs

Select a particular database:

use your_database_name

Show a list of all collections in the current database:

show collections

Importing data into MongoDB

The mongoimport command will import data into a MongoDB collection. You can import data structured as either JSON or CSV files.

Notes:

  • on our MongoDB server, your_username and your_database_name are the same as your CIMS/i6 username.
  • you must first have loaded the MongoDB environment
  • run these commands from the *NIX shell prompt, not from the mongo client prompt

JSON

Here is a sample of importing a document that contains data in JSON format:

mongoimport --db your_database_name --collection your_collection_name --host class-mongodb.cims.nyu.edu --username your_username --password your_password --type json --file your_json_file_name 

CSV

mongoimport --db your_database_name --collection your_collection_name --host class-mongodb.cims.nyu.edu --username your_username --password your_password --type csv --headerline --file your_csv_file_name 

Note that using the --headerline option means that your CSV file must include the field names as the first line of the file.

Quit the MongoDB client

To quit the MongoDB client when you are finished, type CTRL+C or 'exit'.

Helpful links


What links here