CSc 337 LECTURE 33: DATABASES Databases Database (DB) - an - - PowerPoint PPT Presentation

csc 337
SMART_READER_LITE
LIVE PREVIEW

CSc 337 LECTURE 33: DATABASES Databases Database (DB) - an - - PowerPoint PPT Presentation

CSc 337 LECTURE 33: DATABASES Databases Database (DB) - an organized collection of data. We have used files with data separated by new lines By this definition, our files can be considered a database. Database management system (DBMS) -


slide-1
SLIDE 1

CSc 337

LECTURE 33: DATABASES

slide-2
SLIDE 2

Databases

Database (DB) - an organized collection of data.

  • We have used files with data separated by new lines
  • By this definition, our files can be considered a database.

Database management system (DBMS) - software that handles the storage, retrieval, and updating of data.

  • Examples: MongoDB, MySQL, PostgreSQL, etc.
  • Usually when people say "database", they mean data that is

managed through a DBMS.

slide-3
SLIDE 3

Why use a database/DBMS?

  • fast: can search/filter a database quickly compared to a file
  • scalable: can handle very large data sizes
  • reliable: mechanisms in place for secure transactions, backups, etc.
  • built-in features: can search, filter data, combine data from multiple

sources

  • abstract: provides layer of abstraction between stored data and app(s)
  • Can change where and how data is stored without needing to change the

code that connects to the database

  • Some services like Heroku will not permanently save files, so using fs will

not work

slide-4
SLIDE 4

Disclaimer

Databases and DBMS is a huge topic in CS with multiple courses dedicated to it What we will cover:

  • How one particular DBMS works (MongoDB)
  • How to use MongoDB with NodeJS
slide-5
SLIDE 5

MongoDB

MongoDB: A popular open-source DBMS

  • A document-oriented database as opposed to a relational database

Relational database:

Name School Employer Occupation Lori null Self Entrepreneur Malia Harvard null null { name: "Lori", employer: "Self",

  • ccupation: "Entrepreneur"

} { name: "Malia", school: "Harvard" }

Document-oriented DB: Relational databases have fixed schemas; document-oriented databases have flexible schemas

slide-6
SLIDE 6

Setup

  • MongoDB is another software program running on the computer
  • We will use libraries in NodeJS to communicate with the MongoDB
  • For development, we will have 2 processes running:
  • node will run the main server program on port 3000
  • mongod will run the database server on a port 27017
slide-7
SLIDE 7

MongoDB Terminology

Database:

  • A container of MongoDB collections

Collection:

  • A group of MongoDB documents.
  • (Table in a relational database)

Document:

  • A JSON-like object that represents one instance of a collection (Row in a relational database)
  • Also used more generally to refer to any set of key-value pairs
slide-8
SLIDE 8

Mongo-DB Example

Database: ecards-db Collection: card Documents

{ "_id" : ObjectId("5922b8a186ebd7 3e42b1b53c"), "style" : "july4", "message" : "Dear Chip,\n\nHappy 4th of July!\n\n❤Dale" } { "_id" : ObjectId("5922acf09e7640 3b3a7549ec"), "style" : "graduation", "message" : "Hi Pooh,\n\nĸ Congrats!!! ĸ\n\n } { "_id" : ObjectId("5922b90d86ebd7 3e42b1b53d"), "style" : "fathersday", "message" : "HFD" }

The document keys are called fields

slide-9
SLIDE 9

Using MondoDB with NodeJS

You will need to install another node module, MongoDB NodeJS Driver: npm install mongodb

slide-10
SLIDE 10

Installing MongoDB

You will need to install MongoDB on your computer. Mac Instructions Windows instructions

  • Do not follow the part of the instructions that tells you to install Windows Server. Skip this, it will work

fine without it.

Linux installation

  • We assume that you know what you are doing
slide-11
SLIDE 11

Running MongoDB

Type mongod on the command line to run the MongoDB server. If this doesn't work you may need to use the full location of

  • MongoDB. On Windows this is usually

"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"

slide-12
SLIDE 12

Importing a database

Run the following command in the command line replacing database_name, collection_name and json_file_name with appropriate values. mongoimport --db database_name --collection collection_name

  • -drop --file json_file_name

If your machine doesn't recognize mongoimport use the following instead: "C:\Program Files\MongoDB\Server\3.6\bin\mongoimport.exe" MongoDB must be set running in a separate terminal window in order for importing to work.

slide-13
SLIDE 13

mongodb Objects

The mongodb Node library provides objects to manipulate the database, collections, and documents:

  • Db: Database; can get collections using this object
  • Collection: Can get/insert/delete documents from this

collection via calls like insertOne, find, etc.

  • Documents are not special classes; they are just JavaScript objects
slide-14
SLIDE 14

Testing an import worked correctly

Run mongo in the command line (or "C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe") show dbs

  • lists the databases available on the system

use databaseName

  • switches to the database with databaseName

show collections

  • shows all of the collections in the current database

db.collectionName.find()

  • lists everything in the collection with collectionName
slide-15
SLIDE 15

Connecting to a database with NodeJS

const MongoClient = require("mongodb"); const MONGO_URL = 'mongodb://localhost:27017/'; var db = null; var collection = null; MongoClient.connect(MONGO_URL, function(err, client) { db = client.db('imdb'); collection = db.collection('movies'); });

  • The end of the connection string specifies the database name we

want to use.

  • If a database of that name doesn't already exist, it will be created

the first time we write to it.

slide-16
SLIDE 16

Finding a result in the database

function query(year, collection) { const doc = { "year" : year }; collection.findOne(doc, function (err, result) { console.log(result); }); } finds the first result in the database