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


Openings / Announcements / Reminders

MongoDB Blog Posting

Upcoming classes

Additional places available at Hackademy


Class Notes

Related reading for this class:  GetMEAN: Chap. 5


Modeling Data (pp. 130-134)

Review from last class ...

Understanding Mongoose (pp. 131-133)

New material ...

Another way to look at Mongoose terms from Valeri Karpov’s post on blog.mongodb.org

Review material again ...

Documents and Schemas (p. 132-133)

sample document:

{
  "firstname" : "Simon",
  "surname" : "Holmes",
  _id : ObjectId("52279effc62ca8b0c1000007")
}

corresponding schema:

{
  firstname : String,
  surname : String
}

Choosing path names

The properties object

Eight schema types


Totally new material begins here ...

Defining Schemas (p. 134-137)

from page 125 and covered in our last class:

  1. create file db.js in loc8r/app_server/models with the following content
  2. var mongoose = require( 'mongoose' ) ;

note:  this is file loc8r/app_api/models/db.js in the code on Holmes’s GitHub repository

  1. require this file in loc8r/app.js
  2. require( './app_server/models/db' ) ;

Now, to create the schema:

  1. instantiate a new schema
    var locationSchema = new mongoose.Schema({ });

Review the data required

Write a schema to match the data


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}


[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

The schema



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

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)

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

The next command was executed very soon after installing a fresh copy of MongoDB

The commands from here down were executed on my system at home

General query


Creating a MongoDB database (pp. 148-151)

“You don’t actually have to create a MongoDB database; you just start to use it.”

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.

To add to the database, use the save function

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


Updating a MongoDB document


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( {} )

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



This is document http://jesseheines.com:8080/~heines/91.462/91.462-2015-16s/462-lecs/lecture10.jsp.  It was last modified on Friday, August 26, 2022 at 4:09 PM.
Copyright © 2022 by Jesse M. Heines.  All rights reserved.  May be freely copied or excerpted for educational purposes with credit to the author.