UMass Lowell Dept. of Computer Science
COMP 4620 — GUI Programming II
Spring 2016 Semester, Section 201
Prof. Jesse M. Heines
Notes for Class No. 10
Introduction to Assignment No. 2 and
Using MongoDB with Your Projects (continued)
Tuesday, February 23, 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
- Assignment No. 2
Openings / Announcements / Reminders
MongoDB Blog Posting
- The MEAN Stack: Mistakes You’re Probably Making With MongooseJS, And How To Fix Them
Upcoming classes
- see Class Notes page
- for next Tuesday’s class (March 1st) you will be on your own, as I will be away at a conference
- you are to test each other’s project alpha versions
- that is, you are to both test others’ projects and have others test yours
- I will prepare a Google form for you each to write
- your assessment of the projects you tested
- what you learned from others testing your project
- I will be back for next Thursday's class (March 3rd)
- project alpha presentation schedule in Assignment No. 2
- these start on Tuesday, March 8th
Additional places available at Hackademy
- Dyn (www.dyn.com) is holding its fourth annual Hackademy event (www.dyn.com/hackademy).
- Hackademy is a 4-day, invitation only, event that provides an opportunity for selected college students to attend software development workshops and learn new and exciting technologies during the first half of the event.
- During the second half of the event, students will use what they learned during the first part of the event to compete in teams developing apps that can be deployed.
- Corporate video on Dyn: http://www.youtube.com/watch?v=0i_tBeKiwJE&feature=youtu.be
Class Notes
Related reading for this class: GetMEAN: Chap. 5
Modeling Data (pp. 130-134)
Review from last class ...
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.”
New material ...
Another way to look at Mongoose terms from Valeri Karpov’s post on blog.mongodb.org
- “A schema is an object that defines the structure of any documents that will be stored in your MongoDB collection; it enables you to define types and validators for all of your data items.”
- “A connection is a fairly standard wrapper around a database connection.”
- “A model is an object that gives you easy access to a named collection, allowing you to query the collection and use the Schema to validate any documents you save to that collection.”
- “It is created by combining a schema, a connection, and a collection name.”
- “A document is an instantiation of a model that is tied to a specific document in your collection.”
Review material again ...
Documents and Schemas (p. 132-133)
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
Totally new material begins here ...
Defining Schemas (p. 134-137)
from page 125 and covered in our last class:
- 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' ) ;
Now, to create the schema:
- instantiate a new schema
var locationSchema = new mongoose.Schema({ });
Review the data required

Write a schema to match the data

- “If your array will only contain one schema type, such as
String, then you can simply define it by wrapping the schema type in square brackets.”
Assigning default values (p. 136)
rating: {type: Number, "default": 0}
Adding validation (p. 136)
name: {type: String, required: true}
rating: {type: Number, "default": 0, min: 0, max: 5}
- the key here is that such validation is done before accessing the database, thus increasing efficiency significantly
- this sure looks a lot like the validation we saw with the jQuery Validation plugin, doesn’t it?
[As stated previously in class, I am skipping all discussion in the text about geolocation]
Schemas with subdocuments (pp. 138-145)
What we’re modeling

The data

- Subdocuments are used to store repeating, nested data.
- “Subdocuments are very much like documents in that they have their own schema and each is given a unique _id by MongoDB when created.”
- “But subdocuments are nested inside a document and can only be accessed as a path of that parent document.”
The schema
- opening hours has its own schema

- “Note that this schema needs to be in the same file as the
locationSchema definition, and, importantly, it must be before the locationSchema definition.”
- the
openingTimesSchema can then be referenced in the locationSchema definition

- as represented in the MongoDB datavase

“Compiling” schemas into models (pp. 145-146)

- using the default connection name eliminates the need for the third parameter
- add the following line just below the
locationSchema definition
mongoose.model( 'Location', locationSchema ) ;
Using the MongoDB Shell (pp. 147-151)
Current version is 3.2
Don’t forget that MongoDB (mongod) must be running before your run mongo (the command line shell)
- otherwise, you get an error like the following
C:\ > mongo
MongoDB shell version: 3.2.0
connecting to: test
2016-02-22T13:48:00.266-0500 W NETWORK [thread1]
Failed to connect to 127.0.0.1:27017,
reason: errno:10061 No connection could be made because the target machine actively refused it.
2016-02-22T13:48:00.267-0500 E QUERY [thread1]
Error: couldn't connect to server 127.0.0.1:27017, connection
attempt failed :
connect@src/mongo/shell/mongo.js:224:14
@(connect):1:6
exception: connect failed
Running mongo (p. 147)
The first four commands were executed on my system at school
- environment variable
computername = Abraham
The next command was executed very soon after installing a fresh copy of MongoDB
- this is why there are only two entries here
- I added a space between them
simply for clarity
The commands from here down were executed on my system at home
- environment variable
computername = Judy2
- note the passing of a query object as a parameter to the
find() function to limit the result set
- note that the query object is itself a JSON object
- note the addition of the
.pretty() function to format the output
General query
- and then of course you can add
.pretty()
Creating a MongoDB database (pp. 148-151)
“You don’t actually have to create a MongoDB database; you just start to use it.”
- this is different from a relational database system
The book instructs us to use the command:
> use Loc8r
and says that if we then run
> show dbs
we will see the Loc8r database listed.
- unfortunately, this is not true
- the database only appears in the list of
dbs after something has been added to it
To add to the database, use the save function
- note that what you are saving is a JSON object
- you can enter the object as a JavaScript object literal
- note that if you press the Enter key before the object is complete, Mongo prompts you to continue with
...
- if everything is OK, you get the confirmation
WriteResult({ "nInserted" : 1 })
or however many objects you added
- note that the
_id field is added automagically
- note that this field has a type of
ObjectID
Once something has been added to the database, it then shows up when you list all the databases in MongoDB
Special note regarding the code at the bottom of page 149
- MongDB version 3.0 uses
2dsphereIndexVersion 3 for geolocation
- the version of MongoDB on Heroku uses
2dsphereIndexVersion 2
- version 2 is a superset of version 1, so Heroku can use version 1, as well
- therefore if you use geolocation as discussed in the book, you need to add a
2dsphereIndexVersion field to the code at the bottom of page 149
- add the following line just above the
coords line
...
"2dsphereIndexVersion" : 2,
coords: [-0.9690884, 51.455041],
...
- see my extensive post on this at https://forums.manning.com/posts/list/37454.page#p104616
- also see the follow-up responses to my post, including Simon Holmes’s “thank you”
Updating a MongoDB document

- note that there are two arguments
- the first is a query to select the object to update
- the second is the instruction on what to do to that object
- the
$push instruction adds to the document as shown above
- the
$set instruction changes an existing field
> db.locations.update( {
... "_id" : ObjectId("568ed5a47b7bc5849af02031") },
... { $set: { "coords" : [-71.360811,42.622935] } } )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
- “Note the
new Date command for setting the timestamp of the review. Using this ensures that MongoDB stores the date as an ISO date object, not a string — this is what our schema expects and allows greater manipulation of dates data.”
Additional MongoDB shell commands that I have found very useful
To delete all data from a collection (so you can start over)
> db.collection_name.deleteMany( {} )
- BE CAREFUL!
- this command is executed immediately and there is no way to recover your data!
- note the empty object as a parameter, which matches all objects
- note that this does not delete the collections, it just deletes all their data
To delete (drop) a collection
> db.collection_name.drop()
To delete (drop) an entire database
> use db_name
> db.dropDatabase()
This brings us up to getting the database on Heroku
The instructions in the book are straightforward and you should be able to follow them
Remember that you have to register a credit card with Heroku to use the database feature, but it wil not be charged
A couple of corrections to and/or notes on the textbook
- on page 153,
$ heroku addons:add mongolab
should be
$ heroku addons:create mongolab
- on page 154, the mkdir and mongodump commands use Linux path examples
- these are fine on the Mac, but they”re obviously different on Windows
- on page 157, the database URI is wrapped
- in reality, the entire URI must be on one line
- on page 158, under Testing on Heroku, the last of the following 4 lines is missing