0% found this document useful (0 votes)
27 views6 pages

Introduction To Mongodb Solutions 2

ajhhajhsdasdjkhahkhkhdasd

Uploaded by

Mantily Holmes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

Introduction To Mongodb Solutions 2

ajhhajhsdasdjkhahkhkhdasd

Uploaded by

Mantily Holmes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to MongoDB: Solutions

W. H. Bell

October 31, 2022

1 Typesetting and spaces

A fixed width font is used for commands or source code. To aid the reader, a small bucket character
is used to indicate spaces in commands or source code. This character should be replaced with
a space when the command or source code is entered. An example of the bucket that is used to
indicate spaces is demonstrated in Listing 1, where there is a single space between the command and
argument.

Listing 1: Demonstrating the bucket character used to indicate spaces in source code and commands.
command argument

2 Solutions

Several steps in the lab exercises do not have associated questions. These solutions either address
explicit questions or important observations.

• 9. “Add another document to MongoDB. First, try to add a document to the ’Customers’
collection. Then try to add another collection to the database.”

Listing 2 and 3 illustrate how to add an additional document and collection, respectively.

A collection is only created if one document is added to it. Therefore, it is necessary to create a
collection and add at least one document.

Introduction to MongoDB: Solutions Page 1 of 6


Listing 2: Creating an additional document.
1 # !/ usr / bin / env python3
2 import mongo_connect
3
4 # Get a client connection to the MongoDB database .
5 client = mongo_connect . get_client ()
6
7 # Create a connection to the database .
8 db = client [ mongo_connect . ge t_data base_n ame () ]
9
10 # Create a collection .
11 customers = db [ ’ Customers ’]
12
13 # Create a document , which is a Python dictionary .
14 customer = { " CustomerId " : " 1 " ,
15 " CustomerName " : " Barry Rayner " }
16
17 # Insert the document into the collection .
18 id = customers . insert_one ( customer )
19
20 # Insert another document .
21 customer = { " CustomerId " : " 2 " ,
22 " CustomerName " : " Mhairi McKay " }
23 id = customers . insert_one ( customer )

Introduction to MongoDB: Solutions Page 2 of 6


Listing 3: Creating an additional collection and document.
1 # !/ usr / bin / env python3
2 import mongo_connect
3
4 # Get a client connection to the MongoDB database .
5 client = mongo_connect . get_client ()
6
7 # Create a connection to the database .
8 db = client [ mongo_connect . ge t_data base_n ame () ]
9
10 # Create a collection .
11 customers = db [ ’ Customers ’]
12
13 # Create a document , which is a Python dictionary .
14 customer = { " CustomerId " : " 1 " ,
15 " CustomerName " : " Barry Rayner " }
16
17 # Insert the document into the collection .
18 id = customers . insert_one ( customer )
19
20 # Create another collection and add one document to it .
21 books = db [ ’ Books ’]
22 book = { " BookId " : " 1 " ,
23 " BookName " : " A brief history of time . " }
24 id = books . insert_one ( book )

Introduction to MongoDB: Solutions Page 3 of 6


• 14. “Select planets by another document field.”

The Listing 4 selects planets with mean temperatures that are less than zero degrees centigrade.
Line 25 contains the filter, whereas Line 26 includes the list of fields to print and the sort definition.
Sorting is not needed to answer this question, but was added to make it clearer which planets
are being selected.

Listing 4: Selecting planets with mean temperatures that are less than 0◦ C.
1 # !/ usr / bin / env python3
2 import mongo_connect
3 import sys
4
5 # Create a client connection to the MongoDB database .
6 client = mongo_connect . get_client ()
7
8 # Get a connection to the database .
9 db = client [ mongo_connect . ge t_data base_n ame () ]
10
11 # Read the available collection names .
12 print ( " >> Available collections : " )
13 for collection_name in db . l i s t _ c o l l e c t i o n _ n a m e s () :
14 print ( collection_name )
15 print ()
16
17 if " OrbitData " not in db . l i s t _ c o l l e c t i o n _ n a m e s () :
18 print ( " !! Error : the \" OrbitData \" collection was not found . " )
19 sys . exit (1)
20
21 orbit_data = db [ " OrbitData " ]
22
23 # Print planets that are normally below zero degrees C .
24 print ( " >> Planets that are normally below zero degrees C : " )
25 for planet in orbit_data . find ({ ’ Mean Temperature ( C ) ’: { ’ $ lt ’: 0}
},
26 { ’ name ’:1 , ’ Mean Temperature ( C ) ’:2 , ’ _id ’ :0}) . sort ( ’ Mean
Temperature ( C ) ’) :
27 print ( planet )

Introduction to MongoDB: Solutions Page 4 of 6


• 15. “Order planets by their mass.”

Listing 5 demonstrates how to print all planets, ordered by their mass. The search command on
Line 25 includes no filters and a list of fields to print. The sort command follows this, using the
mass of the planet.

Listing 5: Selecting planets ordered by their mass.


1 # !/ usr / bin / env python3
2 import mongo_connect
3 import sys
4
5 # Create a client connection to the MongoDB database .
6 client = mongo_connect . get_client ()
7
8 # Get a connection to the database .
9 db = client [ mongo_connect . ge t_data base_n ame () ]
10
11 # Read the available collection names .
12 print ( " >> Available collections : " )
13 for collection_name in db . l i s t _ c o l l e c t i o n _ n a m e s () :
14 print ( collection_name )
15 print ()
16
17 if " OrbitData " not in db . l i s t _ c o l l e c t i o n _ n a m e s () :
18 print ( " !! Error : the \" OrbitData \" collection was not found . " )
19 sys . exit (1)
20
21 orbit_data = db [ " OrbitData " ]
22
23 # Print the planets , ordered by their mass .
24 print ( " >> The names of the planets , ordered by mass : " )
25 for planet in orbit_data . find ({} , { ’ name ’:1 , ’ Mass (10^{24} kg ) ’:2 , ’
_id ’ :0}) . sort ( " Mass (10^{24} kg ) " ) :
26 print ( planet )
27 print ()

• 17. “What happened the second time that the program was run?”

It produces the error message that is given in Listing 6 and refuses to insert another document
with the same CustomerName. Listing the contents of the Customers collection shows that only
one document exists. This demonstrates that an index can be used to prevent the insertion of
documents with duplicate values in one document field.

Introduction to MongoDB: Solutions Page 5 of 6


Listing 6: Insertion error message.
Warning : could not insert the customer document .

• 18. “What happens this time?”

Once the database has been created and created again, the customer document can be inserted
as before.

Introduction to MongoDB: Solutions Page 6 of 6

You might also like