/u2_lab_mongoDB_sneakers

In this exercise, we'll use the mongo shell to interact with a database of sneakers.

MongoDB Sneaker Exercise

AirForce1s

Overview

In this lab, we'll used what we learned about working in the mongo shell to practice interacting with a new database of sneakers.

Getting started

  • Fork and Clone
  • Open in VSCode so you can paste your answers into the ReadMe
  • Open Terminal
  • Tighten those laces

Instructions

Below is an array of objects:

const products = [
  { model: "4281", brand: "Nike", color: "Red" },
  { model: "4363", brand: "Nike", color: "Black" },
  { model: "4365", brand: "Nike", color: "White" },
  { model: "4490", brand: "Nike", color: "White" },
  { model: "130014", brand: "Nike", color: "Cream" },
  { model: "4361", brand: "Nike", color: "White" },
  { model: "130169", brand: "Nike", color: "Teal" },
  { model: "4401", brand: "Nike", color: "Maroon" },
  { model: "130182", brand: "Nike", color: "Gold" },
]

Let's start by connecting to the mongo interactive shell:

mongosh

Next, let's create a new database:

use sneakersDatabase

Cool, now let's populate our products database with products. Let's start by creating a products collection, then using insertMany to insert multiple documents:

db.products.insertMany([
  { model: "4281", brand: "Nike", color: "Red" },
  { model: "4363", brand: "Nike", color: "Black" },
  { model: "4365", brand: "Nike", color: "White" },
  { model: "4490", brand: "Nike", color: "White" },
  { model: "130014", brand: "Nike", color: "Cream" },
  { model: "4361", brand: "Nike", color: "White" },
  { model: "130169", brand: "Nike", color: "Teal" },
  { model: "4401", brand: "Nike", color: "Red" },
  { model: "4281", brand: "Nike", color: "Gold" },
])

Let's confirm that this insert many operation worked:

db.products.find({})

We should see all our products in our database.

Exercise

Use the MongoDB lesson to solve for the following:

  1. Find all the Air Jordans that have the model number: 4363.
<--- solution goes here !--->
  1. Find all shoes that are either red or black.
<--- solution goes here !--->
  1. Insert 4 new Air Jordans into our collection (you can just make up model #s and colors if you like):
<--- solution goes here !--->
  1. Update all red Jordans to Maroon:
<--- solution goes here !--->
  1. Delete all model 4281 Air Jordans.
<--- solution goes here !--->

Bonus

Create a better representation of a "product".

Maybe a product should have size, and size should be an array of sizes or perhaps a product should have a quantity.

For example:

{ model: "4281", brand: "Nike", color: "Red", sizes: [7,8,9,10,11], qty: 36 },

Once you have a better representation of a product, apply the changes to every product in the database.

Write your queries below:

<--- solution goes here !--->

Jordan

Resources