Introduction To Mongodb Solutions 2
Introduction To Mongodb Solutions 2
W. H. Bell
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.
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 )
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.
• 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.
Once the database has been created and created again, the customer document can be inserted
as before.