Download the MongoChef client
1. Like SQLite can be interacted with through a text-based command line interface or through a graphical interface, so can MongoDB. We will use a client called MongoChef.
2. Download it here: http://3t.io/mongochef/download/core/platform/
3. Install MongoChef and open it.
4. Click the big screen “Connect” button:
5. In the Connection Manager, click “New Connection.”
6. Click From URI…
7.
Enter the
following exactly as specified:
mongodb://user:pass@ds115918.mlab.com:15918/cs340
8. Connect and you should see the cs340 DB appear.
Familiarize yourself
with the addresses collection
1.
To search a mongo collection, you use a JSON
document.
2.
Double-click on the “addresses” collection under
“cs340” and you should see this screen:
3.
Under “Query”, put the following:
{"state": "DC"}
4.
Press the blue circle with the triangle inside
of it to the right (looks like a “play” button) and the query runs. You should see THREE results.
5. Try switching between Table View, Tree View, and JSON View.
6. By default, MongoDB assumes equality when you write {key: value} in a query. To get another kind of comparison, you can use the following query operators:
7.
8. Here’s how they work:
a.
Find all documents where the children field is
greater than 1:
{"children": {$gt: 1}}
b.
Find all people who don’t live in DC:
{"state": {$ne: "DC"}}
c.
Find all people who live in a certain set of
states:
{"state": {$in: ["DC", "TX", "NC"]}}
9. Now we will learn how to use AND and OR in MongoDB.
10. To
combine two queries with AND, just use a comma:
{"children": 0, "state": "DC"}
11. To
combine queries with an OR, use the $or operator, which takes an array of
documents:
{$or : [ {"children": 0}, {"state": "DC"} ] }
12. Here
is a query that finds all people with greater than 2 children or live in a
certain set of states:
{ $or :
[
{"children": {$gt: 1}},
{"state": {$in: ["DC", "TX", "NC"]}}
]
}
STOP
AND ANSWER LAB QUESTIONS PART A!
13. You
can use “dot notation” to search embedded documents. Take a look at the “executive”
collection. This contains JSON
information for all US presidents and vice presidents.
Find all presidents: {"terms.type": "prez"}
Find all vice presidents: {"terms.type":
"viceprez"}
Find all democratic presidents: {"terms.type": "prez", "terms.party": "Democratic"}
Find all republican VPs: {"terms.type": "viceprez",
"terms.party": "Republican"}
Notice how the above are all queries on elements from ARRAYS. When you use the dot notation with arrays,
you are asking that AT LEAST ONE element of the array matches the query.
STOP AND ANSWER LAB QUESTIONS PART B!
Lab
Questions: Put these answers in a
document (Word, PDF, text, whatever) and upload to Moodle.
PART A
Using the "restaurants" data set, write queries to find the following. Write just the
query below for each problem, don't turn in the JSON returned. Use the number of
documents returned to check your work.
1. Find all restaurants located in the Bronx. (2338)
2. Find all bakeries located in the Bronx. (71)
3. Find all bakeries located in either the Bronx or Queens. (275)
PART B:
4. Find all US Presidents with the first name of John. (4) Remember that the executive collection also has vice presidents!
5. [Switch to the curcongress collection.] Find all female members of Congress. (109)
6. Find all Democratic members of Congress representing Tennessee. (2)