Skip to content
Snippets Groups Projects
Commit 0b3114ec authored by William Bell's avatar William Bell
Browse files

Providing more information and correcting the variable names.

parent f9516b92
No related branches found
Tags vm-only
No related merge requests found
No preview for this file type
...@@ -8,7 +8,13 @@ MongoDB provides interfaces for several programming languages, as well as a \tex ...@@ -8,7 +8,13 @@ MongoDB provides interfaces for several programming languages, as well as a \tex
It is assumed that MongoDB has been installed and is running on the local computer (\texttt{localhost}) that is being used to run these examples. It is also assumed that the local computer is running Linux. It is assumed that MongoDB has been installed and is running on the local computer (\texttt{localhost}) that is being used to run these examples. It is also assumed that the local computer is running Linux.
\begin{enumerate} \begin{enumerate}
\item Open a terminal window. \item Open a terminal window on the Linux machine. It is assumed that the Linux machine is using the Bash shell. Therefore, the window will be a Bash prompt.
\item At the Bash prompt, change to the directory that contains the Python files by typing the command given in Listing~\ref{listing:cd-dir}. This command assumes that the Bash prompt is already within the MongoDB examples directory.
%
\begin{lstlisting}[caption={Changing directory.},label=listing:cd-dir,numbers=none,language=Bash]
cd python
\end{lstlisting}
\item At the Bash prompt, type the text given in Listing~\ref{listing:create-database-run}. \item At the Bash prompt, type the text given in Listing~\ref{listing:create-database-run}.
% %
...@@ -17,30 +23,32 @@ It is assumed that MongoDB has been installed and is running on the local comput ...@@ -17,30 +23,32 @@ It is assumed that MongoDB has been installed and is running on the local comput
\end{lstlisting} \end{lstlisting}
% %
The contents of \texttt{create\_database.py} are given in Listing~\ref{listing:create_database.py}. This Python program opens a client connection to MongoDB, assuming that it is running on the local computer. It then creates a database called \texttt{'sales'}. Line 9 either creates the database or forms a connection to it, if it already exists. A collection is created in a similar manner as a database. A document is defined as a Python dictionary at Line 15 and 16. Finally, the Python dictionary is passed to the function \texttt{insert\_one} to insert it into the database. The contents of \texttt{create\_database.py} are given in Listing~\ref{listing:create_database.py}. This Python program opens a client connection to MongoDB, assuming that it is running on the local computer. It then creates a database called \texttt{'sales'}. Line 9 either creates the database or forms a connection to it, if it already exists. A collection is created in a similar manner as a database. A document is defined as a Python dictionary at Line 15 and 16. Finally, the Python dictionary is passed to the function \texttt{insert\_one} to insert it into the database.
\clearpage
\lstinputlisting[caption={The file create\_database.py.},label=listing:create_database.py,language=python]{"../python/create_database.py"} \lstinputlisting[caption={The file create\_database.py.},label=listing:create_database.py,language=python]{"../python/create_database.py"}
\item At the Bash prompt, execute the script \texttt{read\_database.py} in a similar manner as Listing~\ref{listing:create-database-run}. \item At the Bash prompt, execute the script \texttt{read\_database.py} in a similar manner as Listing~\ref{listing:create-database-run}.
The contents of \texttt{read\_database.py} are given in Listing~\ref{listing:read_database.py}. This Python program opens a client connection to MongoDB. Then it gets the \texttt{'sales'} database. It loops over the collection names in the database, printing each collection name. If the \texttt{'Customers'} collection does not exist, an Error message is printed and the program stops. If the \texttt{'Customers'} collection does exist, then each document is printed that is within the collection. The \texttt{find} function is used at Line 24 without arguments, selecting all documents and fields within the collection. The contents of \texttt{read\_database.py} are given in Listing~\ref{listing:read_database.py}. This Python program opens a client connection to MongoDB. Then it gets the \texttt{'sales'} database. It loops over the collection names in the database, printing each collection name. If the \texttt{'Customers'} collection does not exist, an Error message is printed and the program stops. If the \texttt{'Customers'} collection does exist, then each document is printed that is within the collection. The \texttt{find} function is used at Line 24 without arguments, selecting all documents and fields within the collection.
% \clearpage
\lstinputlisting[caption={The file read\_database.py.},label=listing:read_database.py,language=python]{"../python/read_database.py"} \lstinputlisting[caption={The file read\_database.py.},label=listing:read_database.py,language=python]{"../python/read_database.py"}
\item Add another document to MongoDB. First, try to add a document to the \texttt{'Customers'} collection. Then try to add another collection to the \texttt{'sales'} database. Finally, try adding another database, collection and document. (The \texttt{drop\_databases.py} script can be executed to clear MongoDB if needed. The \texttt{drop\_databases.py} script can be executed in a similar manner as Listing~\ref{listing:create-database-run}.) \item Add another document to MongoDB. First, try to add a document to the \texttt{'Customers'} collection. Then try to add another collection to the \texttt{'sales'} database. Finally, try adding another database, collection and document. (The \texttt{drop\_databases.py} script can be executed to clear MongoDB if needed. The \texttt{drop\_databases.py} script can be executed in a similar manner as Listing~\ref{listing:create-database-run}.)
\item At the Bash prompt, execute the script \texttt{planets\_database.py} in a similar manner as Listing~\ref{listing:create-database-run}. \item At the Bash prompt, execute the script \texttt{planets\_database.py} in a similar manner as Listing~\ref{listing:create-database-run}.
The contents of \texttt{planets\_database.py} are given in Listing~\ref{listing:planets_database.py}. This program creates a \texttt{'planets'} database and a \texttt{'orbitData'} collection. It reads JSON data from a text file called \texttt{'planets.json'} and inserts these data into MongoDB. The contents of \texttt{planets\_database.py} are given in Listing~\ref{listing:planets_database.py}. This program creates a \texttt{'planets'} database and a \texttt{'OrbitData'} collection. It reads JSON data from a text file called \texttt{'planets.json'} and inserts these data into MongoDB.
The first section of the \texttt{'planets.json'} file is given in Listing~\ref{listing:planets.json}. This file contains a list, where each element is a dictionary. The Python program reads these data into a list that contains dictionaries as its elements. The \texttt{insert\_many} function is called at Line 21 to insert these data into the MongoDB database. The first section of the \texttt{'planets.json'} file is given in Listing~\ref{listing:planets.json}. This file contains a list, where each element is a dictionary. The Python program reads these data into a list that contains dictionaries as its elements. The \texttt{insert\_many} function is called at Line 21 to insert these data into the MongoDB database.
\clearpage
\lstinputlisting[caption={The file planets\_database.py.},label=listing:planets_database.py,language=python]{"../python/planets_database.py"} \lstinputlisting[caption={The file planets\_database.py.},label=listing:planets_database.py,language=python]{"../python/planets_database.py"}
\lstinputlisting[caption={The first 24 lines of the file planets.json.},label=listing:planets.json,language=java,lastline=24]{"../python/planets.json"} \lstinputlisting[caption={The first 24 lines of the file planets.json.},label=listing:planets.json,language=java,lastline=24]{"../python/planets.json"}
\item At the Bash prompt, execute the script \texttt{select\_planet.py} in a similar manner as Listing~\ref{listing:create-database-run}. \item At the Bash prompt, execute the script \texttt{select\_planet.py} in a similar manner as Listing~\ref{listing:create-database-run}.
The contents of \texttt{select\_planet.py} are given in Listing~\ref{listing:select_planet.py}. This Python program prints the names of the collections and then checks if the \texttt{'orbitData'} collection is present. If the \texttt{'orbitData'} collection is present, it prints the names of each of the planets and the complete document for Pluto. The contents of \texttt{select\_planet.py} are given in Listing~\ref{listing:select_planet.py}. This Python program prints the names of the collections and then checks if the \texttt{'orbit\_data'} collection is present. If the \texttt{'OrbitData'} collection is present, it prints the names of each of the planets and the complete document for Pluto.
The \texttt{find} function at Line 27 includes two arguments, which are each given within \texttt{\{\}} parentheses. The first pair of \texttt{\{\}} parentheses is empty. This is the condition, which is optional. Since the condition is empty, all documents are considered. The second argument is \texttt{\{'name':1, '\_id':0 \}}. The second argument is a list of document fields that should be considered. The format is the field name, a colon and the position number. Setting the position number to zero for the \texttt{\_id} field causes it not to be printed. The \texttt{find} function at Line 27 includes two arguments, which are each given within \texttt{\{\}} parentheses. The first pair of \texttt{\{\}} parentheses is empty. This is the condition, which is optional. Since the condition is empty, all documents are considered. The second argument is \texttt{\{'name':1, '\_id':0 \}}. The second argument is a list of document fields that should be considered. The format is the field name, a colon and the position number. Setting the position number to zero for the \texttt{\_id} field causes it not to be printed.
...@@ -50,7 +58,7 @@ The \texttt{find\_one} function at Line 33 includes a condition that the field ' ...@@ -50,7 +58,7 @@ The \texttt{find\_one} function at Line 33 includes a condition that the field '
\item At the Bash prompt, execute the script \texttt{planets\_with\_moons.py} in a similar manner as Listing~\ref{listing:create-database-run}. \item At the Bash prompt, execute the script \texttt{planets\_with\_moons.py} in a similar manner as Listing~\ref{listing:create-database-run}.
The contents of \texttt{planets\_with\_moons.py} are given in Listing~\ref{listing:planets_with_moons.py}. This program reads data from the \texttt{'orbitData'} collection. The \texttt{find} function at Line 27 is used to list the names of the planets and their number of moons. The \texttt{find} function at Line 34 contains two arguments. The text \texttt{\$gt} implies greater than. Therefore, the condition is that the selected documents must have more than zero moons. The second argument is used to print the planet name and number of moons. Finally, the \texttt{sort} function is used to sort the documents by the number of moons. The contents of \texttt{planets\_with\_moons.py} are given in Listing~\ref{listing:planets_with_moons.py}. This program reads data from the \texttt{'OrbitData'} collection. The \texttt{find} function at Line 27 is used to list the names of the planets and their number of moons. The \texttt{find} function at Line 34 contains two arguments. The text \texttt{\$gt} implies greater than. Therefore, the condition is that the selected documents must have more than zero moons. The second argument is used to print the planet name and number of moons. Finally, the \texttt{sort} function is used to sort the documents by the number of moons.
\lstinputlisting[caption={The file planets\_with\_moons.py.},label=listing:planets_with_moons.py,language=python]{"../python/planets_with_moons.py"} \lstinputlisting[caption={The file planets\_with\_moons.py.},label=listing:planets_with_moons.py,language=python]{"../python/planets_with_moons.py"}
...@@ -74,9 +82,7 @@ The comparison query operators are provided in Table~\ref{table:comp-operators} ...@@ -74,9 +82,7 @@ The comparison query operators are provided in Table~\ref{table:comp-operators}
\end{tabular} \end{tabular}
\end{center} \end{center}
\end{table} \end{table}
\clearpage \clearpage
\item Select planets by another document field. \item Select planets by another document field.
\item Order planets by their mass. \item Order planets by their mass.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment