Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active December 11, 2022 06:11
Show Gist options
  • Save ross-u/74bc350d4eb8ac4b1ca1034f0a12e2e8 to your computer and use it in GitHub Desktop.
Save ross-u/74bc350d4eb8ac4b1ca1034f0a12e2e8 to your computer and use it in GitHub Desktop.
MongoDB | CRUD Operations - Create and query a collection - Exercise

MongoDB | CRUD Operations


img


Create and query a collection - Exercise


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 3: List all employees. You may want to check the methods .find() and .pretty()

  • Task 4: Find all employees whose name is 'Steve'.

  • Task 5: Find all employees whose age is greater than 40.

  • 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:


  1. On your system, navigate to the desired directory and save the file `employees.json` provided below.
  2. Open the terminal and navigate to the same directory of the `employees.json` file that you just saved.
  3. Run the below command in the terminal (not mongo shell) to import the documents in to the database from the json file:
  4. mongoimport --db=my-first-db --collection=employees --file=employees.json --jsonArray
[
{
"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 }
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment