In this lab, we'll used what we learned about working in the mongo shell to practice interacting with a new database of sneakers.
Fork
andClone
- Open in VSCode so you can paste your answers into the ReadMe
- Open Terminal
- Tighten those laces
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.
Use the MongoDB lesson to solve for the following:
- Find all the Air Jordans that have the model number: 4363.
<--- solution goes here !--->
- Find all shoes that are either red or black.
<--- solution goes here !--->
- Insert 4 new Air Jordans into our collection (you can just make up model #s and colors if you like):
<--- solution goes here !--->
- Update all red Jordans to Maroon:
<--- solution goes here !--->
- Delete all model 4281 Air Jordans.
<--- solution goes here !--->
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 !--->