Giter VIP home page Giter VIP logo

dsc-mongodb-lab-onl01-dtsc-ft-012120's Introduction

MongoDB - Lab

Introduction

In this lesson, we'll get some hands-on experience with MongoDB!

Objectives

You will be able to:

  • Create a MongoDB database
  • Insert data into a MongoDB database
  • Read data from a MongoDB database
  • Update data in a MongoDB database

Getting Started

To begin this lab, make sure that you start up the mongoDB server in your terminal first! You must do this lab locally on your computer, not in Learn.

Connecting to the MongoDB Database

In the cell below, import the appropriate library and connect to the mongoDB server. Create a new database called 'lab_db'.

Creating a Collection

Now, create a collection called 'lab_collection' inside 'lab_db'.

mycollection = None

Adding Some Data

Now, we're going to add some example records into our database. In the cells below, create dictionary representations of the following customer records:

Name Email Mailing_Address Balance Notes
John Smith [email protected] 123 mulberry lane 0.0 Called technical support, issue not yet resolved
Jane Smith [email protected] Null 25.00 Null
Adam Enbar [email protected] 11 Broadway 14.99 Set up on recurring billing cycle
Avi Flombaum [email protected] 11 Broadway 0.0 Null
Steven S. [email protected] Null -20.23 Refunded for overpayment due to price match guarantee

Your first challenge is to take all of the data in the table above and create the corresponding documents and add then to our mongo database. Note that fields that contain 'Null' should not be included in the document (recall that since mongo is schema-less, each document can be different).

Create the documents from the table listed above, and then use insert_many() to insert them into the collection.

customer_1 = None
customer_2 = None
customer_3 = None
customer_4 = None
customer_5 = None

all_records = None

insertion_results = None

Now, access the appropriate attribute from the results object returned from the insertion to see the unique IDs for each record inserted, so that we can confirm each were inserted correctly.

Querying and Filtering

In the cell below, return the name and email address for every customer record. Then, print every item from the query to show that it worked correctly.

query_1 = None

Great! Now, let's write a query that gets an individual record based on a stored key-value pair a document contains.

In the cell below, write a query to get the record for 'John Smith' by using his name. Then, print the results of the query to demonstrate that it worked correctly.

query_2 = None

Great! Now, write a query to get the names, email addresses, and balances for customers that have a balance greater than 0. Use a modifier to do this.

HINT: In the query below, you'll be passing in two separate dictionaries. The first one defines the logic of the query, while the second tells which fields we want returned.

query_3 = None

Updating a Record

Now, let's update some records. In the cell below. set the mailing address for 'John Smith' to '367 55th St., apt 2A'.

record_to_update_1 = None
update_1 = None

Now, write a query to check that the update worked for this document in the cell below:

query_4 = None

Now, let's assume that we want to add birthdays for each customer record. Consider the following table:

Name Birthday
John Smith 02/20/1986
Jane Smith 07/07/1983
Adam Enbar 12/02/1982
Avi Flombaum 04/17/1983
Steven S. 08/30/1991

We want to add birthdays for each person, but we want to do so in a way where we don't have to do the same repetitive task over and over again. This seems like a good opportunity to write a function to handle some of the logic for us!

In the cell below:

  • Store the names in the names_list variable as a list.
  • Store the birthdays in the birthdays_list variable as a list.
  • Write a function that takes in the two different lists, and updates each record by adding in the appropriate key-value pair containing that user's birthday.

Hint: There are several ways that you could write this, depending on whether you want to use the update_one() method or the update_many() method. See if you can figure out both approaches!

names_list = None
birthdays_list = None

def update_birthdays(names, birthdays):
    pass
        
update_birthdays(names_list, birthdays_list)

Now, write a query to check your work and see that the birthdays were added correctly.

Great! It looks like the birthdays have been successfully added to every record correctly!

Summary

In this lesson, we got some hands-on practice working with MongoDB!

dsc-mongodb-lab-onl01-dtsc-ft-012120's People

Contributors

loredirick avatar mike-kane avatar sumedh10 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.