Skip to content

Commit

Permalink
Some updates to MongoDB Guided Practice
Browse files Browse the repository at this point in the history
  • Loading branch information
lalpert authored Mar 1, 2019
1 parent 86c9d3d commit 2a91ca0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions databases/mongo-db.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Not all data can be or should be stored in a relational way. In those cases, it

- [TutorialsPoint MongoDB Tutorial](http://www.tutorialspoint.com/mongodb/)
- [MongoDB Full Tutorial w/ Node.js, Express, and Mongoose](https://youtu.be/4yqu8YF29cU)
- [MongoDB Shell commands reference](https://docs.mongodb.com/manual/reference/mongo-shell/)

### Lesson

Expand All @@ -53,7 +54,7 @@ If no Homebrew-related text appears, you'll need to install Homebrew. Go to the

3. Install MongoDB using `brew install mongodb` in the Terminal. If you get an error message saying you need to install Xcode from the App Store, follow the instructions to do so and then re-try `brew install mongodb`. Ask for help if needed.

4. Make sure you have the directory /data/db. If not, run `sudo mkdir -p /data/db`. If this doesn't work, then try `brew services start mongodb`
4. MongoDB will store data in a directory called /data/db. Check if you have the directory /data/db. If not, run `sudo mkdir -p /data/db`. If this doesn't work, then try `brew services start mongodb`
5. Run `whoami` to find your username. For example, if your username is "myname", then you will run the following command `sudo chown myname /data/db`. You may need to enter your password.
6. If a ~/.bash_profile exists, open it. If not, create one by `touch .bash_profile`. Open your .bash_profile in your text editor and copy the following into the file:
- `export MONGO_PATH=/usr/local/mongodb`
Expand All @@ -63,19 +64,21 @@ If no Homebrew-related text appears, you'll need to install Homebrew. Go to the
8. Keep the first window open with `mongod` still running. Switch to second terminal window, run `mongo --host 127.0.0.1:27017`. This is your Mongo shell.

**Activity 2: Working with Database**
- In the shell, run `use techtonica`. Then run `show dbs`, which will list out the list of databases. What database is there? What is missing?
- The command `use <db>` sets the current database you'll be working with. In the shell, run `use techtonica`. Then run `show dbs`, which will list out the list of databases. What database is there? What is missing?

- The newly created database, "techtonica", should be missing. This is because the techtonica database is empty. Insert a document by running `db.classmates.insert({"name": "your_name", "month": your_birthmonth})`. Do it at least two more times with a friend's name. Remember not to double-quote the birth month to keep it as a digit instead of string.
- The newly created database, "techtonica", should be missing. This is because the `techtonica` database is empty. Let's insert some data storing people's name (as a string) and birth month (as an integer). Insert a document by running `db.classmates.insert({"name": "your_name", "month": your_birthmonth})`. Do it at least two more times with a friend's name. Remember not to double-quote the birth month to keep it as a digit instead of string.

- MongoDB stores documents in collections. Run `show collections`. What is already there? Can you guess at least where one of the collections came from?
- Type `show dbs` again and see that the `techtonica` database now shows up.

- MongoDB stores documents in collections. Run `show collections`. What is already there? Where did it come from?

- Run `db.createCollection("volunteer")`.

- View what's in each collection by running `db.classmates.find()` and `db.volunteers.find()`. What's the difference?

- Fix the empty volunteer collection by entering a document: `db.volunteers.insert({"volunteer": "Adrien"})` and `db.volunteers.insert({"volunteer": "Jamie"})`.

- Select a number between your birth month and your pair's birth month inside the classmates collection. Run `db.classmates.remove({"month": {$lt: the_number_your_selected}})`. The "$lt" means "less than." What do you think happened to the collections?
- Now we'll try out the `remove` command. Select a number between your birth month and your pair's birth month inside the classmates collection. Run `db.classmates.remove({"month": {$lt: the_number_your_selected}})`. The "$lt" means "less than." What do you think happened to the collections?

- Run `db.classmates.find()`. Were you correct?

Expand Down

0 comments on commit 2a91ca0

Please sign in to comment.