Introduction to the MongoDB Client and UMongo GUI

Introduction to the MongoDB Client and UMongo GUI
You have been provided with a user account on the MongoDB server at the University. Once you
have connected to the database and provided your credentials, you will be able to start storing and
retrieving data. You will be using two different methods to access the database. The simplest,
though perhaps not easiest, method is to use the command line in the Mongo shell. Once you are
confident at creating JSON style queries and using Javascript to manipulate results, you will find the
command line the quickest way to get things done.
However, the command line provides little help when you are starting out, so we will use a GUI
called UMongo. You will need to work out how to achieve the tasks set out in this and future
practicals – the answers are not on this sheet. The lecture notes are a good place to start, as is the
MongoDB web site. You will also find a lot of answers on StackOverflow.
Run both the command line and UMongo
UMongo is a Java program, which you will find on the lab computers.
To connect, select File->Connect and enter the details of the Mongo DB you want to connect to. For
us, they are:
•
•
•
•
•
Name: Whatever you want to call it – this is just used to identify the connection to you later
Server: mqr1.cs.stir.ac.uk
Database: The username you were given
User: The username you were given
Password: The password you were given
You can run the mongo shell from a DOS command line. Get one by typing cmd into the program
search bar. Once in the DOS shell, type:
mongo –host mqr1.cs.stir.ac.uk –u <username> -p <password> <dbname>
to run the MongoDB client.
Start a New Collection
Start a new collection by typing db.createCollection(‘books’) into the shell. Now look in UMongo and
see that a new collection has appeared.
Add Some Data
Back at the commend line, now try your JSON building skills by adding the following books (below is
NOT JSON format – you need to construct that yourself). Use db.books.insert() to add each
one.
• title=Learning Python, author=Lutz, price=30
• title=”NoSQL Distilled”, authors = Sadalage and Fowler (add the authors as an array), price = 40
Search the Database
Type db.books.find() and see what you get. Not pretty, but you can see what you entered.
Now return to the UMongo GUI and have a look there. Work out how to get a nice JSON format
representation of the DB contents.
Back at the command line, work out how to run a query to perform the following:
1.
2.
3.
4.
5.
Find the book called ‘NoSQL Distilled’
List the titles of all books
Find the author of the book called ‘Learning Python’
List all books with a price of less than 35
List all the books with Fowler as an author
Update an Entry
Now write some code to change the price of NoSQL Distilled to 25 and verify that the change has
been made with an appropriate query.
I have intentionally left the price as a number to allow range comparisons (less than 30, for example)
but we might want to store the currency (£ or $ for example). For this, we can make the price entry a
JSON object of its own:
{“amount”:50,”currency”:”Dollars”}
1. Now update your two entries to reflect this idea, making the Python book $30 and the
NoSQL book £40. Do this as two separate updates.
2. Now write a query that finds all the books with a price in pounds.
3. Now update the Python book so that it has one price in pounds and another in dollars and
write a query that returns only the price in pounds. You will need to read this:
http://docs.mongodb.org/manual/reference/operator/projection/positional/
To select the right price from the array. Think carefully about how you will represent the two
different values – you cannot have two fields with the same name.
Delete an Entry
Add another entry with title=”Oops”. Now delete the same entry. Notice how the JSON part of the
query is the same.
Using the UMongo GUI
Now that we have done something with the command line, let’s see how it works with the UMongo
GUI. UMongo expects you to know what parameters a query needs and requires you to express it in
JSON, just as you would from the command line.
Find()
Select your books collection and choose Find from the Document menu. The short cut is Ctrl+Shift+F.
You will be presented with boxes in which you can enter JSON for the query, the fields you want
returned (i.e. projection), fields to sort on, and some options for the number of entries returned and
some other things we will return to later.
1. Try at first simply clicking Ok without entering anything in the dialog. This is equivalent to
typing find()
a. Look at the list of results. You can expand an entry by clicking its little + sign in the
tree.
b. Click the txt button to see the result in raw JSON format. This is handy to cut and
paste from later
2. Bring the find dialog back and enter the JSON required to find the details of the book called
Learning Python. Note you are not typing Mongo commands, so you don’t need
books.find(..). Just enter the JSON you want the query to match.
3. Now use the fields box to select just the author of the Python book
4. Finally, find the price of the Python book
Insert()
The Document->Insert menu presents you with a dialog in which you type the JSON to insert a new
entry. Enter a third book called Databases by Richie, with a price of 30 pounds or 40 dollars.
Use find() again to make sure it was entered correctly.
Arrays
Next, let’s add a customer collection to record who has which books. Use uMongo or the command
line, whichever you want, to:
1. Create a collection called customers
2. Insert a record for a customer with name=”Tom”
3. Record the fact that Tom has bought the books “Learning Python” and “Databases” using an
array entry in the document describing Tom
4. Use find() again to make sure it was entered correctly
5. Write a query to perform the following:
a.
b.
c.
d.
Find everyone who has bought “Databases”
Find everyone who has bought “Databases” and “Learning Python”
Find everyone who has bought “Databases” or “Learning Python”
How would you find the authors of the books Tom bought?