Skip to content

Latest commit

 

History

History
230 lines (169 loc) · 11.5 KB

CS167-Lab7.md

File metadata and controls

230 lines (169 loc) · 11.5 KB

Lab 7

Objectives

  • Understand the document database model.
  • Manipulate a set of documents in a database.
  • Understand how MongoDB deals with the flexibility of the document data model.

Prerequisites


Lab Work

I. Setup MongoDB and Database Tools (20 minutes)

  • Note: If you use the provided virtual machine, you will find that MongoDB is pre-installed.
  1. Download the corresponding archive files (either .zip or .tgz) according to your system.

  2. Extract the downloaded MongoDB archive file to your course directory cs167.

    • Linux (Ubuntu): ~/cs167/mongodb-linux-x86_64-ubuntu2004-5.0.8
    • macOS: ~/cs167/mongodb-macOS-x86_64-5.0.8
    • Windows: C:\cs167\mongodb-win32-x86_64-windows-5.0.8
  3. Extract the downloaded MongoDB database tools archive file, copy or move all the files inside the bin directory to the installed MongoDB's bin directory. Available files (on Windows, those should have .exe extension) are:

    • bsondump
    • mongoexport
    • mongoimport
    • mongostat
    • mongodump
    • mongofiles
    • mongorestore
    • mongotop
  4. Configure environment variables.

    • Linux (Ubuntu):
      1. Add export MONGODB_HOME="/home/$LOGNAME/cs167/mongodb-linux-x86_64-ubuntu2004-5.0.8"
      2. Add $MONGODB_HOME/bin to PATH. Separator is :
      3. Reload the profile via source command or restart the terminal
    • macOS:
      1. Add export MONGODB_HOME="/Users/$LOGNAME/cs167/mongodb-macOS-x86_64-5.0.8"
      2. Add $MONGODB_HOME/bin to PATH. Separator is :
      3. Reload the profile via source command or restart the terminal
    • Windows:
      1. Add a user variable with name MONGODB_HOME and value C:\cs167\mongodb-win32-x86_64-windows-5.0.8
      2. Add %MONGODB_HOME%\bin to Path variable.
      3. Restart the terminal.
  5. Create a $MONGODB_HOME/data directory where your data will be stored.

    • Linux and macOS: mkdir $MONGODB_HOME/data
    • Windows: mkdir "%MONGODB_HOME%\data" for CMD or mkdir "$Env:MONGODB_HOME\data" for PowerShell and Windows terminal
  6. Start the MongoDB server by running the following command (you must keep the tab/window open while doing this lab).

    • Linux and macOS

      mongod --dbpath $MONGODB_HOME/data
    • Windows CMD

      mongod --dbpath "%MONGODB_HOME%\data"
    • Windows PowerShell or Windows Terminal

      mongod --dbpath "$Env:MONGODB_HOME\data"

    On macOS, if you see the following error, click Cancel.

    Run the following command (you must be a system administrator to use sudo).

    sudo spctl --master-disable

    Then rerun the mongod command above. Once it starts, you can run the following command to revert the changes.

    sudo spctl --master-enable

    See more details about macOS GateKeeper.


II. Data Manipulation (60 minutes)

  1. Import the sample file into a new collection named contacts. You will need to use mongoimport command from the database tool. You may use --collection and --jsonArray two options.

    • (Q1) What is your command?
    • (Q2) What is the output of the above command?
  2. Retrieve all the users sorted by Name (default order).

    • (Q3) What is your command?

    Copy the output to a file named q3.txt. Your .txt file should look like

    { ... }
    { ... }
    { ... }

    Or with cursor.pretty()

    {
        ...
    }
    {
        ...
    }
    { 
        ...
    }

    Hint: Use db.collection.find() and cursor.sort().

  3. List only the _id and Name sorted in reverse alphabetical order by Name (Z-to-A).

    • (Q4) What is your command?

    Copy the output to q4.txt. The file format should be the same as step 2. The output should not contain other attributes other than _id and Name.

    Hint: You will need to use projection and Ascending/Descending Sort.

  4. (Q5) Is the comparison of the attribute Name case-sensitive?

    Show how you try this with the previous query and include your answer.

    Hint: To check if a comparison is case sensitive, there must be at least one string with upper case letter and one string with lower case letters. For example, given "Apple" and "Berry", "Apple" < "Berry" in both case sensitive and insensitive comparisons. However, if you have "apple" and "Berry", it will be "apple" > "Berry" in a case sensitive comparison and "apple" < "Berry" in a case insensitive comparison. Note that you cannot tell "Apple" and "berry" because 'A' < 'b' in both case sensitive and insensitive comparisons.

    You may check ASCII table.

  5. Repeat step 3 above but do not show the _id field.

    • (Q6) What is your command?

    Copy the output to q6.txt using the same format. The output should only contain attribute Name.

  6. Insert the following document to the collection.

    {Name: {First: "David", Last: "Bark"}}
    
    • (Q7) Does MongoDB accept this document while the Name field has a different type than other records?
    • (Q8) What is your command?
    • (Q9) What is the output of the above command?

    Hint: Use db.collection.insertOne().

  7. Rerun step 3, which lists the records sorted by Name.

    • (Q10) Where do you expect the new record to be located in the sort?
  8. Insert the following document into the contacts collection.

    {Name: ["David", "Bark"]}
    
    • (Q11) What is your command?
    • (Q12) What is the output of the above command?
  9. Rerun step 3.

    • (Q13) Where do you expect the new document to appear in the sort order. Verify your answer and explain after running the query.
  10. Rerun step 3, but this time sort the Name in ascending order.

    • (Q14) Where do you expect the last inserted record, {Name: ["David", "Bark"]} to appear this time? Does it appear in the same position relative to the other records? Explain why or why not.

    Copy the output to q14.txt. The file format should be the same as step 2. The output should not contain other attributes other than _id and Name.

    Hint: Ascending/Descending Sort.

  11. Build an index on the Name field for the contacts collection.

    • (Q15) Is MongoDB able to build the index on that field with the different value types stored in the Name field?
    • (Q16) What is your command?
    • (Q17) What is the output of the above command?

    Hint: Use db.collection.createIndex().


III. Submission (2 minutes)

  1. Write your answers using the template README.md file.

  2. Make a .tar.gz or .zip file with README.md, q3.txt, q4.txt, q6.txt and q14.txt.

    Your archive file should be:

    <UCRNetID>_lab7.{tar.gz | zip}
      - README.md
      - q3.txt
      - q4.txt
      - q6.txt
      - q14.txt
    
  3. Do not forget to include your information as you do in other labs.

  4. No separate code is required for this lab.