CSc 337
LECTURE 33: DATABASES
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) -
LECTURE 33: DATABASES
Database (DB) - an organized collection of data.
Database management system (DBMS) - software that handles the storage, retrieval, and updating of data.
managed through a DBMS.
sources
code that connects to the database
not work
Databases and DBMS is a huge topic in CS with multiple courses dedicated to it What we will cover:
MongoDB: A popular open-source DBMS
Relational database:
Name School Employer Occupation Lori null Self Entrepreneur Malia Harvard null null { name: "Lori", employer: "Self",
} { name: "Malia", school: "Harvard" }
Document-oriented DB: Relational databases have fixed schemas; document-oriented databases have flexible schemas
Database:
Collection:
Document:
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
You will need to install another node module, MongoDB NodeJS Driver: npm install mongodb
You will need to install MongoDB on your computer. Mac Instructions Windows instructions
fine without it.
Linux installation
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
"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"
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
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.
The mongodb Node library provides objects to manipulate the database, collections, and documents:
collection via calls like insertOne, find, etc.
Run mongo in the command line (or "C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe") show dbs
use databaseName
show collections
db.collectionName.find()
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'); });
want to use.
the first time we write to it.
function query(year, collection) { const doc = { "year" : year }; collection.findOne(doc, function (err, result) { console.log(result); }); } finds the first result in the database