csc 337
play

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) -


  1. CSc 337 LECTURE 33: DATABASES

  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.

  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

  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

  5. MongoDB MongoDB : A popular open-source DBMS ◦ A document-oriented database as opposed to a relational database Relational database: Document-oriented DB: Name School Employer Occupation { name: "Lori", Lori null Self Entrepreneur employer: "Self", Malia Harvard null null occupation: "Entrepreneur" } { Relational databases have fixed name: "Malia", schemas; document-oriented school: "Harvard" databases have flexible schemas }

  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

  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

  8. Mongo-DB Example Documents { "_id" : ObjectId("5922b8a186ebd7 3e42b1b53c"), "style" : "july4", "message" : "Dear Chip,\n\nHappy 4th of July!\n\n ❤ Dale" } Database: Collection: { "_id" : ObjectId("5922acf09e7640 3b3a7549ec"), "style" : ecards-db card "graduation", "message" : "Hi Pooh,\n\ nĸ Congrats!!! ĸ \n\n } { "_id" : ObjectId("5922b90d86ebd7 The document keys are called fields 3e42b1b53d"), "style" : "fathersday", "message" : "HFD" }

  9. Using MondoDB with NodeJS You will need to install another node module, MongoDB NodeJS Driver : npm install mongodb

  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

  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"

  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.

  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

  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

  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.

  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

Recommend


More recommend