UMass Lowell Dept. of Computer Science
COMP 4620 — GUI Programming II
Spring 2016 Semester, Section 201
Prof. Jesse M. Heines
Notes for Class No. 9
Kate Carcia Visit and Introduction to Using MongoDB with Your Projects
Thursday, February 18, 2016
A video of this class is (or will be) posted at:
http://echo360.uml.edu/heines2016/comp4620-201.html
Handouts and Materials
Links related to today’s class
- my version of Holmes’s app on the Heroku server
- Holmes’s GitHub repository
- the best of the longtitude and latitude finders referenced by Holmes in Chapters 5 and 6
Openings / Announcements / Reminders
LinkedIn Posting: Gone in Six Seconds: Why I Passed on Your Résumé
- note in particular reasons 2 and 3
For those of you who know Angular1 and want to look at Angular2
Address by Alumnae Kate Carcia
Class Notes
Related reading for this class: GetMEAN: Chap. 5
Things to Note as You Work Through Simon Holmes’s Textbook
- when you use
console.log for debugging, the messages show up in the nodemon window, not the browser console
- things went pretty smoothly for me through Chapter 6, but as soon as I hooked up my database in Chapter 7 I got very strange results from the geo location routines
- this was even though I mostly cut-and-pasted from Holmes’s code on GitHub repository
- note that the code on GitHub isn’t always the same as the code in the book
- this is due to the lag in publishing a book
- the only way I could get the geo location routines working properly was to download the full chapter-06 branch from his repository
- I did this by downloading it as a ZIP file, not cloning it
- in either case, be sure to do an
npm install in your installation directory once you have the code on your system
- thus, it is important that you begin each chapter with fully working code from the previous chapter
- it is incredibly important that geo locations are entered to his 2-element array as [longtitude,latitude]
- it is easy to reverse these, because most sites specify latitude before longtitude
- for example, when you search by coordinates on Google maps, latitude precedes longtitude
- thus, for Simon Holmes’s code, 198 Riverside Street is
[lng,lat] = [-71.3263192,42.654584]
- for Google maps, the search must be entered as is
42.654584,-71.3263192
The “chicken and egg” problem revisited
- three weeks until spring break — 6 classes after today
- I will be out of town for the class on Tuesday, March 1st — I am planning short quiz on the NodeJS material for that day
- we also want to have presentations of project alpha versions before spring break
- therefore, you need to push on with the textbook on your own and raise questions on Piazza and in class
- remember that other students may answer your questions faster — and even better — than I can
Comments on lecture and lecture notes techniques are welcomed
- remember: this material is new to me, too
Building a Data Model with MongoDB and Mongoose (Ch. 5, p. 120)
Reviewing what we’re doing (p. 121)

Approach (p. 121)

How things fit together (p. 122)

Step 1: Install MongoDB (Appendix A, p. 393)
- download and install from https://www.mongodb.org
- versions for Windows, Linux, Mac OS X, Solaris
- seems to automagically detect your system and select the appropriate version
- note that MongoDB can be installed as a Windows service and made to start automatically when Windows boots
Step 2: Install Mongoose (p. 123)
$ npm install --save mongoose
- the
--save option adds Mongoose to your package.json file
- Mongoose provides the ability to define data structures and models
- validation can also be included
- “MongoDB only talks to Mongoose, and Mongoose in turn talks to Node and Express.”
- note that “Mongoose isn’t installed globally, but is instead added directly to our application.”
Step 3 and 4: Add Mongoose to our application (p. 125)
- create file
db.js in loc8r/app_server/models with the following content
var mongoose = require( 'mongoose' ) ;
note: this is file loc8r/app_api/models/db.js in the code on Holmes’s GitHub repository
- based on the discussion in the book I do not know why it is in a different location
- require this file in loc8r/app.js
require( './app_server/models/db' ) ;
Step 5: Restart the application or make sure you’re running nodemon to restart it automagically (p. 125)
- ensure that the application runs without error
Step 6: Create the Mongoose connection (p. 125)

- the username, password, and port are all optional
- 27027 is the default port number
- on your local machine, you just need
localhost as your server address and your database name
- thus, the commands you need in
loc8r/app_server/models/db.js are:
var dbURI = 'mongodb://localhost/Loc8r' ;
mongoose.connect(dbURI) ;
- note that MongoDB must have been started for these commands to work
Steps 7 and 8: Monitoring the connection with Mongoose connection events (p. 126)
- add the following statements to
loc8r/app_server/models/db.js
source: https://github.com/simonholmes/getting-MEAN/blob/chapter-05/app_server/models/db.js
- when the application is restarted, you should see:
in the nodemon window
Steps 9 and 10: Closing a Mongoose connection (p. 126-128)
- “... the Mongoose connection doesn’t automatically close when the application stops or restarts. We need to listen for changes in the Node process to deal with this.”
- this is typical of connectiosn to relational databases, too
- “Closing the Mongoose connection when the application stops is as much a part of the best practice as opening the connection when it starts.”
- “To monitor when the application stops we need to listen to the Node.js process, listening for an event called SIGINT.”
- the textbook talks about what to do if this even does not fire on Windows, but I found that it does fire on Windows 10
- “If you’re using nodemon to automatically restart the application then you’ll also have to listen to a second event on the Node process called SIGUSR2. Heroku uses another different event, SIGTERM, so we’ll need to listen for that as well.”
- wrap the
connection.close command in the following statements to loc8r/app_server/models/db.js
source: https://github.com/simonholmes/getting-MEAN/blob/chapter-05/app_server/models/db.js
- call the above function when an appropriate signal is detected
Note that “the nodemon listener is using process.once as opposed to process.on, as we only want to listen for the SIGUSR2 event once. nodemon also listens for the same event and we don’t want to capture it each time, preventing nodemon from working.”
The complete database connection file loc8r/app_server/models/db.js is shown in Listing 5.1 on p. 129
- as indicated above, it is also of course on Holmes’s GitHub repository in the
chapter-05 branch
Modeling Data (pp. 130-134)
Where we’re going (p. 131)

Understanding Mongoose (pp. 131-133)
- “In MongoDB each entry in a database is called a document.”
- “In MongoDB a collection of documents is called a collection (think “table” if you’re used to relational databases).”
- “In Mongoose the definition of a document is called a schema.”
- “Each individual data entity defined in a schema is called a path.”

- “A model is the compiled version of a schema.“
- “All data interactions using Mongoose go through the model.”
- “We’ll work with models more in chapter 6, but for now we’re focusing on building them.”
Documents and Schemas
sample document:
{
"firstname" : "Simon",
"surname" : "Holmes",
_id : ObjectId("52279effc62ca8b0c1000007")
}
corresponding schema:
{
firstname : String,
surname : String
}

- note that there is no
_id in the schema
- “
_id is the unique identifier — the primary key in relational database terms — for each document.”
- “MongoDB automatically creates this path when each document is created and assigns it a unique
ObjectId value.”
- “The value is designed to always be unique by combining the time since the Unix epoch with machine and process identifiers and a counter.”
Choosing path names
- these will be used in the documents and in your code, so name them well
The properties object
- a true JavaScript object
- “At a minimum this contains the data type, but it can include validation characteristics, boundaries, default values, and more.”
Eight schema types
- String — any string, UTF-8 encoded
- Number — Mongoose doesn’t support long or double numbers, but it can be extended to do so using Mongoose plugins; the default support is enough for most cases
- Date — typically returned from MongoDB as an ISODate object
- what is ISO8601 format again?
- Boolean — true or false
- Buffer — for binary information such as images
- Mixed — any data type
- Array — can either be an array of the same data type, or an array of nested subdocuments
- ObjectId — for a unique ID in a path other than
_id; typically used to reference _id paths in other documents
A Word to the Wise
by Brett McLaughlin, PHP & MySQL: The Missing Manual, p. 137