These exercises can be run using a MongoDB server on the local PC or using a remote MongoDB server. If a remote MongoDB server is used, then the connection details must be provided by setting environment variables as demonstrated in Listing~\ref{listing:envs}, where \texttt{server\_name}, \texttt{username}, \texttt{password} and \texttt{db\_name} should be replaced by the corresponding connection values. These variables must be set in the terminal window where the Python example programs are run. If the environment variables are not set, the default values from Table~\ref{table:env-defaults} are used.
\caption{Default settings for environment variables.}
\label{table:env-defaults}
\begin{tabular}{l|l}\hline
\textbf{Variable}&\textbf{Default/Action}\\
\hline
\texttt{MONGODB\_SERVER}&\texttt{localhost}\\
\texttt{MONGODB\_USERNAME}&\texttt{root}\\
\texttt{MONGODB\_PASSWD}&\texttt{example}\\
\texttt{MONGODB\_DB}&\texttt{test}\\\hline
\end{tabular}
\end{center}
\end{table}
The environment variable settings given in Listing~\ref{listing:envs} can be set for all Bash terminals by editing the \texttt{\~{}/.bashrc} file using a text editor such as \texttt{nano}. The file can be opened as shown in Listing~\ref{listing:nano-bashrc}, following which the lines given in Listing~\ref{listing:envs} should be appended, saving the file by typing \texttt{ctrl-x}, \texttt{y} and \texttt{enter}.
\begin{lstlisting}[caption={Opening the \texttt{.bashrc} file with the nano editor.},label=listing:nano-bashrc,numbers=none,language=Bash,showspaces=true]
nano ~/.bashrc
\end{lstlisting}
An initial version of the environment variables can be written to the \texttt{\~{}/.bashrc} file by running the command given in Listing~\ref{listing:update_bashrc} once the repository has been cloned using Listing~\ref{listing:git-clone}. The \texttt{\~{}/.bashrc} should then be edited, setting the remaining values as needed.
\begin{lstlisting}[caption={Updating the \texttt{\~{}/.bashrc} Bash configuration file.},label=listing:update_bashrc,numbers=none,language=Bash,showspaces=true]
./introduction-to-mongodb/bin/update_bashrc.sh
\end{lstlisting}
The settings that are given in the \texttt{\~{}/.bashrc} file are read when a new Bash window is opened. To cause them to be reread in the current window after the \texttt{\~{}/.bashrc} file has been updated, type the command given in Listing~\ref{listing:reread-bashrc}.
\begin{lstlisting}[caption={Re-reading the \texttt{\~{}/.bashrc} Bash configuration file.},label=listing:reread-bashrc,numbers=none,language=Bash,showspaces=true]
source ~/.bashrc
\end{lstlisting}
The environment variables are read by the functions that are defined in \texttt{mongo\_connect.py}. The functions in \texttt{mongo\_connect.py} are used to connect to the MongoDB database server and select a database.
These exercises can be run using a MongoDB server on the local PC or using a remote MongoDB server. If a remote MongoDB server is used, then the connection details must be provided by setting environment variables as demonstrated in Listing~\ref{listing:envs}, where \texttt{server\_name}, \texttt{username}, \texttt{password} and \texttt{db\_name} should be replaced by the corresponding connection values. These variables must be set in the terminal window where the Python example programs are run. If the environment variables are not set, the default values from Table~\ref{table:env-defaults} are used.
\caption{Default settings for environment variables.}
\label{table:env-defaults}
\begin{tabular}{l|l}\hline
\textbf{Variable}&\textbf{Default/Action}\\
\hline
\texttt{MONGODB\_SERVER}&\texttt{localhost}\\
\texttt{MONGODB\_USERNAME}&\texttt{root}\\
\texttt{MONGODB\_PASSWD}&\texttt{example}\\
\texttt{MONGODB\_DB}&\texttt{test}\\\hline
\end{tabular}
\end{center}
\end{table}
The environment variable settings given in Listing~\ref{listing:envs} can be set for all Bash terminals by editing the \texttt{\~{}/.bashrc} file using a text editor such as \texttt{nano}. The file can be opened as shown in Listing~\ref{listing:nano-bashrc}, following which the lines given in Listing~\ref{listing:envs} should be appended, saving the file by typing \texttt{ctrl-x}, \texttt{y} and \texttt{enter}.
\begin{lstlisting}[caption={Opening the \texttt{.bashrc} file with the nano editor.},label=listing:nano-bashrc,numbers=none,language=Bash,showspaces=true]
nano ~/.bashrc
\end{lstlisting}
An initial version of the environment variables can be written to the \texttt{\~{}/.bashrc} file by running the command given in Listing~\ref{listing:update_bashrc} once the repository has been cloned using Listing~\ref{listing:git-clone}. The \texttt{\~{}/.bashrc} should then be edited, setting the remaining values as needed.
\begin{lstlisting}[caption={Updating the \texttt{\~{}/.bashrc} Bash configuration file.},label=listing:update_bashrc,numbers=none,language=Bash,showspaces=true]
./introduction-to-mongodb/bin/update_bashrc.sh
\end{lstlisting}
The settings that are given in the \texttt{\~{}/.bashrc} file are read when a new Bash window is opened. To cause them to be reread in the current window after the \texttt{\~{}/.bashrc} file has been updated, type the command given in Listing~\ref{listing:reread-bashrc}.
\begin{lstlisting}[caption={Re-reading the \texttt{\~{}/.bashrc} Bash configuration file.},label=listing:reread-bashrc,numbers=none,language=Bash,showspaces=true]
source ~/.bashrc
\end{lstlisting}
The environment variables are read by the functions that are defined in \texttt{mongo\_connect.py}. The functions in \texttt{mongo\_connect.py} are used to connect to the MongoDB database server and select a database.