Data Stores

Abel Sanchez and John R. Williams

Data

Fundamental components of computer programs:

  • A way to represent data
  • A way to store data
  • Instructions to manipulate the data

Data Stores

Stores and manipulates information

Two Types of Data Stores

  • Relational: represents all data as tables
  • Document: data is thought of as a “document”

Database Design

The database design involves choosing:

  • The documents collections (tables)
  • The fields/properties (the columns)
  • How collections and properties interact

Entity-relationship modeling

E/R Modelling is used for conceptual design

  • Entities - objects or things of interest
  • Attributes - facts or properties of an entity
  • Relationships - links between entities

Entities

Represent objects or things of interest

  • Physical things like students, professors, employees
  • Abstractions like courses, programs, projects, orders
  • Have types like course, professor
  • Have instances like courses: calculus, algebra

Entities in Diagrams

Entities are drawn as a box

Attributes

Attributes are facts, properties, or details of an entity.

  • Students have IDs, names, courses
  • Courses have names, credits, level

Attributes in Diagrams

Represented a few different ways.

Relationships

Relationships are an association between two or more entities.

  • A student takes several courses
  • A course is taugh by one professor

Cardinality Ratios

An entity can participate in three types of relationships

  • One to one (1:1). Each professor has one office
  • One to many (1:N). A professor can advice many students
  • Many to many (N:N). Each student takes many classes, and each class is taken my many students.

Relationships in Diagrams

Relationships are links between two entities

Removing N:N Relationships

We can split a many to many relationship into two one to many relationships. A new entity represents the N:N relationship.

Entity-relationship modeling

Look at your data and indentify

  • Entities
  • Attributes
  • Relationships
  • Cardinality ratios

Example

A university consists of a number of departments. Each department offers several programs. A number of courses make up each program. Students enroll in a particular program and take courses towards the completion of that program. Each course is taught by a professor from the appropriate department, and each professor advices a group of students.

Example - Entities

A university consists of a number of departments. Each department offers several programs. A number of courses make up each program. Students enroll in a particular program and take courses towards the completion of that program. Each course is taught by a professor from the appropriate department, and each professor advices a group of students.

Example - ER Diagram

Entities: department, program, course, professor, student

Active Learning

Complete ER Diagram

Sample ER Diagram

Normalization Rules

  • Each row-and-column intersection holds only one value
  • Nonkey columns must depend on the entire primary key
  • No nonkey column depend on another nonkey column

We will use a document database

Advantages of Document Stores

  • Documents are independent units
  • Application logic is easier to write
  • Unstructured data can be stored easily

Active Learning - Install MongoDB

Submit screenshot of running server

(http://www.mongodb.org/)

Path Permissions

You may need to set permissions

Bin

Start Server

mongod

Start Client

mongo

JavaScript & MongoDB

Document

Insert


					db.users.insert(
					      {
					        name :'peter',
					        email:'peter@mit.du',
					        age  :'18'
					      }
					)
					

Insert

Collection

Multiple documents make a collection

Collection

Active Learning - Create Collection

Enter a few documents into your collection. When you are done, list them using db.yourCollectionName.find()

Modify Documents

db.collection.update()


					db.users.update(
					   { email: "peter@mit.edu" },
					   {
					      $set: { email: "parker@mit.edu" }
					   }
					)
					

Modify Multiple Documents

If multi is set to true, all matching documents are updated. Else, only one document is updated.


					db.users.update(
					  {},
					  {$set: { city: "boston" }},
					  { multi: true }
					)
					

Remove Documents


					// removes all documents from a collection, 
					// but does not remove the indexes
					db.collection.remove();

					// drops the entire collection, 
					// including the indexes
					db.collection.drop();
					

Remove On Condition

Remove documents in the users collection with email parker@mit.edu


					db.users.remove( 
					  { 
					    email : "parker@mit.edu" 
					  } 
					)
					

Query Documents

the find method retrieves documents from a collection


					db.collection.find()
					

Select All Documents in a Collection

An empty query document selects all documents in the collection. Not specifying a query document has the same effect.


					// empty query document
					db.users.find( {} );

					// no query document
					db.users.find();
					

Find All

db.users.find()

Specify Condition


					db.users.find( { email : "peter@mit.edu" } );

					// --- output ---
					// { "_id"  : ObjectId("123"), 
					//   "name" : "peter", 
					//   "email": "peter@mit.du", 
					//   "age"  : "18" }
					

Query Operators

Use $in to match field values. The statement below matches documents in the users collection where the name field is peter or clark. For more query operators, see http://docs.mongodb.org/


					db.users.find( { name : { $in : ['peter','clark'] } } )

					// --- output ---
					// { "_id"   : ObjectId("5d"), "name" : "peter", 
					//   "email" : "peter@mit.du", "age"  : "18" }
					// { "_id"   : ObjectId("57"), "name" : "clark", 
					//   "email" : "clark@mit.du", "age"  : "25" }
					

Query

Bulk Insert


					load('yourPath/bulk_insert.js')
					

Bulk Insert


					// load file using - load('yourFile.js')
					var data = [
					  { name : 'dan', email : 'dan@mit.edu'},
					  { name : 'bob', email : 'bob@mit.edu'},
					  { name : 'lou', email : 'lou@mit.edu'},
					  { name : 'meg', email : 'meg@mit.edu'},
					  { name : 'amy', email : 'amy@mit.edu'},
					  { name : 'liv', email : 'liv@mit.edu'},
					];

					var length = data.length;
					for (var i = 0; i < length; i++) {
					  db.students.insert(data[i]);
					}
					

Robomongo

http://robomongo.org/

Robomongo Local

Video - MongoDB Install OSX

Video - MongoDB Install Windows

Video - Data Conversions

Components

  1. JavaScript code on the client
  2. JavaScript code on the server
  3. JavaScript code on the DB
  4. Datasets
  5. GUI
  6. Visualization

The End