In this exercise we are going to create a new documentcollection
and execute few basic queries in MongoDB using the mongo
shell.
Task 1 :
We will work in the current database that we have created for the lecture, and create a new collection named employees
inside it.
Run the below commands in mongo
shell to:
- check what is the name of the current database
- list all of the collections of the current database &
- create a new collection in that database
db
show collections
db.createCollection("employees");
Task 2 :
Copy the below code and run it in the mongo
shell to insert the new documents representing our employees:
db.employees.insertMany(
[
{
name: "Sue",
age: 19,
phone: {
personal: "555-123-123",
work: "555-456-456",
ext: "2342"
},
privileges: "user",
favorites: { artist: "Picasso", food: "pizza"
},
finished: [
17,
3
],
badges: [
"blue",
"black"
],
points: [
{ points: 85, bonus: 20
},
{ points: 85, bonus: 10
}
]
},
{
name: "Steve",
age: 46,
phone: {
personal: "929-884-8752",
work: "555-456-456",
ext: "1109"
},
privileges: "admin",
favorites: { artist: "Rembrandt", food: "kroketten"
},
finished: [
25
],
badges: [
"red",
"blue"
],
points: [
{ points: 12, bonus: 5
},
{ points: 40, bonus: 0
}
]
},
{
name: "Bob",
age: 42,
phone: {
personal: "555-123-123",
work: "555-456-456",
ext: "7673"
},
privileges: "admin",
favorites: { artist: "Miro", food: "meringue"
},
finished: [
11,
25
],
badges: [
"green"
],
points: [
{ points: 85, bonus: 20
},
{ points: 64, bonus: 12
}
]
},
{
name: "Willy",
age: 22,
phone: {
personal: "555-123-123",
work: "555-456-456",
ext: "8263"
},
privileges: "user",
favorites: { artist: "Cassatt", food: "cake"
},
finished: [
6
],
badges: [
"blue",
"Picasso"
],
points: [
{ points: 81, bonus: 8
},
{ points: 55, bonus: 20
}
]
},
{
name: "John",
age: 34,
phone: {
personal: "555-123-123",
work: "555-456-456",
ext: "2143"
},
privileges: "admin",
favorites: { artist: "Chagall", food: "chocolate"
},
finished: [
5,
11
],
badges: [
"Picasso",
"black"
],
points: [
{ points: 53, bonus: 15
},
{ points: 51, bonus: 15
}
]
},
{
name: "Steve",
age: 23,
phone: {
personal: "555-123-123",
work: "555-456-456",
ext: "8253"
},
privileges: "user",
favorites: { artist: "Noguchi", food: "nougat"
},
finished: [
14,
6
],
badges: [
"orange"
],
points: [
{ points: 71, bonus: 20
}
]
},
{
name: "Martin",
age: 43,
phone: {
personal: "555-123-123",
work: "555-456-456",
ext: "5623"
},
privileges: "user",
favorites: { food: "pizza", artist: "Picasso"
},
finished: [
18,
12
],
badges: [
"black",
"blue"
],
points: [
{ points: 78, bonus: 8
},
{ points: 57, bonus: 7
}
]
}
]
);
Once you have introduced the above documents in the collection employees
using the your mongo
shell, perform the following queries:
- Task 4: Find all employees whose
name
is'Steve'
.
- Task 5: Find all employees whose
age
is greater than40
.
-
Task 6: Find the employee whose extension,
ext
field is"2143"
.If you are not sure how to query the nested
ext
field take a look at the docs here : Query on Nested Field
Troubleshooting
If unable to execute Task 2, inserting the documents using `insertMany`, do the following steps:
- On your system, navigate to the desired directory and save the file `employees.json` provided below.
- Open the terminal and navigate to the same directory of the `employees.json` file that you just saved.
- Run the below command in the terminal (not mongo shell) to import the documents in to the database from the json file:
mongoimport --db=my-first-db --collection=employees --file=employees.json --jsonArray