- 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.
- Download the following sample file contacts.json.
- Download MongoDB Community Edition. We will work with version 5.0.8 (current).
- Linux (Ubuntu): Select
Ubuntu 20.04
in Platform andtgz
in Package. Don't select other package types. * You may use the MongoDB for Ubuntu 18.04 if your system is that version. - macOS: Select
macOS
in Platform. The Package has only one optiontgz
. * You may use an older version of MongoDB (4.4 or 4.2) if your system is 10.13 or lower. - Windows: Select
Windows
in Platform andzip
in Package. Don't select themsi
package.
- Linux (Ubuntu): Select
- Download MongoDB Database Tools. The current version is 100.5.2.
- Linux (Ubuntu): Select
Ubuntu 20.04 x86 64
in Platform andtgz
in Package. Don't select thedeb
package. - macOS: Select
macOS x86_64
in Platform. The Package has only one optionzip
. - Windows: Select
Windows x86_64
in Platform andzip
in Package. Don't select themsi
package.
- Linux (Ubuntu): Select
- If you use the provided virtual machine, it comes with MongoDB pre-installed.
- For testing purposes, you can use the online web-based MongoDB version
- Note: If you use the provided virtual machine, you will find that MongoDB is pre-installed.
-
Download the corresponding archive files (either .zip or .tgz) according to your system.
-
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
- Linux (Ubuntu):
-
Extract the downloaded MongoDB database tools archive file, copy or move all the files inside the
bin
directory to the installed MongoDB'sbin
directory. Available files (on Windows, those should have.exe
extension) are:- bsondump
- mongoexport
- mongoimport
- mongostat
- mongodump
- mongofiles
- mongorestore
- mongotop
-
Configure environment variables.
- Linux (Ubuntu):
- Add
export MONGODB_HOME="/home/$LOGNAME/cs167/mongodb-linux-x86_64-ubuntu2004-5.0.8"
- Add
$MONGODB_HOME/bin
toPATH
. Separator is:
- Reload the profile via
source
command or restart the terminal
- Add
- macOS:
- Add
export MONGODB_HOME="/Users/$LOGNAME/cs167/mongodb-macOS-x86_64-5.0.8"
- Add
$MONGODB_HOME/bin
toPATH
. Separator is:
- Reload the profile via
source
command or restart the terminal
- Add
- Windows:
- Add a user variable with name
MONGODB_HOME
and valueC:\cs167\mongodb-win32-x86_64-windows-5.0.8
- Add
%MONGODB_HOME%\bin
toPath
variable. - Restart the terminal.
- Add a user variable with name
- Linux (Ubuntu):
-
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 ormkdir "$Env:MONGODB_HOME\data"
for PowerShell and Windows terminal
- Linux and macOS:
-
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.
-
-
Import the sample file into a new collection named
contacts
. You will need to usemongoimport
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?
-
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()
andcursor.sort()
. -
List only the
_id
andName
sorted in reverse alphabetical order byName
(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
andName
.Hint: You will need to use
projection
and Ascending/Descending Sort. -
(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.
-
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 attributeName
. -
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()
. - (Q7) Does MongoDB accept this document while the
-
Rerun step 3, which lists the records sorted by
Name
.- (Q10) Where do you expect the new record to be located in the sort?
-
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?
-
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.
-
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
andName
.Hint: Ascending/Descending Sort.
- (Q14) Where do you expect the last inserted record,
-
Build an index on the
Name
field for thecontacts
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()
. - (Q15) Is MongoDB able to build the index on that field with the different value types stored in the
-
Write your answers using the template
README.md
file. -
Make a
.tar.gz
or.zip
file withREADME.md
,q3.txt
,q4.txt
,q6.txt
andq14.txt
.Your archive file should be:
<UCRNetID>_lab7.{tar.gz | zip} - README.md - q3.txt - q4.txt - q6.txt - q14.txt
-
Do not forget to include your information as you do in other labs.
-
No separate code is required for this lab.