![Page 1: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/1.jpg)
Produced by
Department of Computing & Mathematics Waterford Institute of Technology
http://www.wit.ie
Web Application Development David Drohan ([email protected])
![Page 2: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/2.jpg)
PART 3
&
![Page 3: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/3.jpg)
Table of Contents 1. Databases Overview 2. MongoDB Overview
§ Installation § Structure and documents § Hosting locally MongoDB § Console CLI § RoboMongo, MongoVUE, Umongo § Executing queries on MongoDB data
3. Mongoose Overview § Mongoose Models § Types of properties
§ Mongoose CRUD operations § save, remove, find
§ Mongoose Queries
010NODE.JSPART3 3
![Page 4: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/4.jpg)
Databases HISTORY & BACKGROUND
![Page 5: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/5.jpg)
Dawn of Databases to Present
Brewer’sCapbornWWW
born
10genfounded
1965 1970 1975 1980 1985 1990 1995 2000 2005 2010
IBM’sIMS
Coddpublishesrela/onalmodelpaperin1970
SQLinvented
Oraclefounded
PC’sgaintracPon
ClientServer
DynamicWebContent
3Perarchitecture
WebapplicaPons
SOA
CloudCompuPng
released
NoSQLMovement
BigTable
![Page 6: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/6.jpg)
Relational Databases
![Page 7: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/7.jpg)
Data stored in a RDBMS is very compact (disk was more expensive) SQL and RDBMS made queries flexible with rigid schemas Rigid schemas helps optimize joins and storage Massive ecosystem of tools, libraries and integrations Been around 40 years!
Relational Databases Strengths
![Page 8: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/8.jpg)
Gartner uses the 3Vs to define § Volume - Big/difficult/extreme volume is relative § Variety ◦ Changing or evolving data ◦ Uncontrolled formats ◦ Does not easily adhere to a single schema ◦ Unknown at design time
§ Velocity ◦ High or volatile inbound data ◦ High query and read operations ◦ Low latency
Enter Big Data
![Page 9: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/9.jpg)
RDBMS Challenges
DATAVARIETY&VOLATILITY• Extremelydifficulttofindasinglefixedschema• Don’tknowdataschemaa-priori
TRANSACTIONALMODEL• NxInsertsorupdates• DistributedtransacPons
• Systemsscalinghorizontally,notverPcally• Commodityservers• CloudCompuPng
VOLUME&NEWARCHITECTURES
![Page 10: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/10.jpg)
Modern NoSQL theory and offerings started in early 2000s " (idea around much longer)
Modern usage of term introduced in 2009 NoSQL = Not Only SQL A collection of very different products Alternatives to relational databases when they are a bad fit Motives ◦ Horizontally scalable ◦ Flexibility
Enter NoSQL
![Page 11: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/11.jpg)
Key-Value Stores ◦ Key-Value Stores ◦ Value (Data) mapped to a key (think primary)
Big Table Descendants ◦ Looks like a distributed multi-dimension map ◦ Data stored in a column oriented fashion ◦ Predominantly hash based indexing
Document oriented stores ◦ Data stored as either JSON or XML documents
NoSQL Databases – Key-Value
![Page 12: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/12.jpg)
2007 ◦ Eliot Horowitz & Dwight Merriman tired of reinventing the wheel ◦ 10gen founded ◦ MongoDB Development begins
2009 ◦ Initial release of MongoDB
73M+ in funding ◦ Funded by Sequoia, NEA (New Enterprise Associates), Union Square Ventures,
Flybridge Capital
10gen & MongoDB
![Page 13: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/13.jpg)
MongoDB OBJECT-DOCUMENT MODEL
![Page 14: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/14.jpg)
MongoDB is an open-source document store database ◦ The leading NoSQL database (see next slide) ◦ Save JSON-style objects with dynamic schemas
Support many features: ◦ Support for indices ◦ Rich, document-based queries (CRUD operations) ◦ Flexible aggregation and data processing ◦ Store files of any size without complicating your stack ◦ Fast in-place updates
Overview: MongoDB – What is it?
010NODE.JSPART3 14
![Page 15: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/15.jpg)
DB-Engines Ranking (Dec 2015)
010NODE.JSPART3 15
![Page 16: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/16.jpg)
§ Data stored as documents (JSON) § Schema free § CRUD operations – (Create Read Update Delete) § Atomic document operations § Ad hoc Queries like SQL
§ Equality § Regular expression searches § Ranges § Geospatial
§ Secondary indexes § Sharding (sometimes called partitioning) for scalability § Replication – HA (High Availability) and read scalability
Overview: MongoDB Key Features
![Page 17: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/17.jpg)
Overview: MongoDB Data Model A MongoDB instance can have many databases ◦ A database can have many collections ◦ A collection can have many documents
MongoDBInstance
Database DatabaseCollec/onDocument
DocumentDatabase
Database
Collec/on Collec/on
Collec/on Collec/on
Collec/on Collec/onDocument
010NODE.JSPART3 17
![Page 18: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/18.jpg)
Overview: MongoDB Documents Documents in MongoDB are JSON objects
ModuleName:Databases
Students:250
Titles:
-IntrotoSQL
-NoSQLdatabases
-EntityFramework
Evalua/on:
-Homework:10
-Exam:60
-Teamwork:20
-ASendance:10
{”module_name":"Databases","students":250,"titles":["IntrotoSQL","NoSQLdatabases","DBPerformance"],"evaluation":{"homework":10,"exam":60,"teamwork":20,"attendance":10}
010NODE.JSPART3 18
![Page 19: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/19.jpg)
Overview: MongoDB Documents MongoDB documents can have many types of values ◦ Numbers, Booleans ◦ Strings, Object Ids ◦ Dates ◦ Objects ◦ Also know as nested documents
◦ Arrays
17,3.14,true/false
"DavidDrohan"
2014-09-01T14:58:48.126Z
{"username":”daveyd","accessLevel":2}
['bananas','oranges']
010NODE.JSPART3 19
![Page 20: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/20.jpg)
MongoDBInstallation INSTALLING & USING MONGODB
![Page 21: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/21.jpg)
Download MongoDB from the official web site: ◦ https://www.mongodb.org/downloads ◦ Installers for all major platforms
How to start MongoDB?
◦ Go to installation folder and run mongod◦ Paths need some configuraHon ◦ You should specify databases path ◦ or use default path and create folder C:\data\db
MongoDB Installation
$cdC:\MongoDb\bin
$mongod––dbpathD:\mongodb\data
$mongod
010NODE.JSPART3 21
![Page 22: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/22.jpg)
Connect to a mongodb Select a database Show database name Show all databases
How to use MongoDB $mongo
$use<databasename>
$db
$showdbs
010NODE.JSPART3 22
![Page 23: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/23.jpg)
How to create users in a database?
Database user roles: ◦ read, readWrite, dbAdmin, dbOwner, userAdmin◦ userAdminAnyDatabase
Login:
Authentication
$usessd4db$db.createUser({"user":"daveyd","pwd":"ssd412345","roles":["readWrite","dbAdmin"]})
db.auth("daveyd","ssd412345")010NODE.JSPART3 23
![Page 24: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/24.jpg)
MongoDBQueries INSTALLING & USING MONGODB
![Page 25: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/25.jpg)
Read query
Same as (in SQL):
MongoDB Queries – Read $db.courses.find({level:{$gt:2}},{module_name:true}).limit(5);
collecPon filters projecPon cursormodifier
SELECTmodule_nameFROMcoursesWHERElevel>2LIMIT5
010NODE.JSPART3 25
![Page 26: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/26.jpg)
Operators: ◦ Comparison – $gt, $gte, $et, $lt, $lte ◦ Logical – $and, $nor, $not, $or◦ Element – $exists, $type
MongoDB Queries – Read (2)
$db.courses.find({$or:[{level:{$gt:1}},{level:{$lt:3}}]});
$db.courses.find({module_name:”Databases"});
010NODE.JSPART3 26
![Page 27: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/27.jpg)
Insert query
Same as (in SQL):
MongoDB Queries – Insert
$db.courses.insert({module_name:"Databases",description:"Databasesdescription",level:3})
INSERTINTOcourses(module_name,description,level)VALUES("Databases","Databasesdescription",3)
010NODE.JSPART3 27
![Page 28: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/28.jpg)
Inserting bulk data in MongoDB MongoDB Queries – Bulk Insert
varbulk=db.courses.initializeUnorderedBulkOp();bulk.insert({name:"ASP.NETMVC",level:3});bulk.insert({item:"WebDevelopment",level:3});bulk.insert({item:"SPAApplications",level:2});bulk.execute();
010NODE.JSPART3 28
![Page 29: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/29.jpg)
Update query
Same as (in SQL):
MongoDB Queries – Update
$db.courses.update({module_name:{$et:"Databases"}},{$set:{description:"Concepts,MSSQL,MySQL,Oracle"}},{multi:true});
UPDATEcoursesSETdescription="Concepts,MSSQL,MySQL,Oracle"WHEREmodule_name="Databases"
010NODE.JSPART3 29
![Page 30: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/30.jpg)
Delete query
Same as (in SQL):
MongoDB Queries – Delete
$db.courses.remove({module_name:"Databases"});
DELETEFROMcoursesWHEREmodule_name="Databases"
010NODE.JSPART3 30
![Page 31: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/31.jpg)
Aggregation is operation that process data records and return computed results
Result:
MongoDB Queries – Aggregation
$db.courses.aggregate([{$match:{isActive:true}},{$group:{level:"$level",students:{$sum:"$studentsCount"}}}])
[{level:1,students:460},{level:2,students:350},{level:3,students:200}]
010NODE.JSPART3 31
![Page 32: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/32.jpg)
MongoDB is an open-source DB system ◦ So there are many available viewers
Some, but not all are: ◦ MongoDB CLI ◦ Comes with installation of MongoDB ◦ Execute queries inside the CMD/Terminal
◦ MongoVUE & UMongo & Robomongo ◦ Provide UI to view, edit are remove DB documents ◦ Execute queries inside the tool
MongoDB Management Tools
010NODE.JSPART3 32
![Page 33: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/33.jpg)
MongoDB - Robomongo
010NODE.JSPART3 33
![Page 34: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/34.jpg)
MongoDB - Robomongo
010NODE.JSPART3 34
![Page 35: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/35.jpg)
Donationweb CASE STUDY EXAMPLE
![Page 36: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/36.jpg)
Launch the MongoDB Server
010NODE.JSPART3 36
![Page 37: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/37.jpg)
Launch the MongoDB Client
010NODE.JSPART3 37
![Page 38: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/38.jpg)
Create/Switch to chosen Database
010NODE.JSPART3 38
![Page 39: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/39.jpg)
Search / Insert Records
010NODE.JSPART3 39
![Page 40: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/40.jpg)
Find All Records
010NODE.JSPART3 40
![Page 41: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/41.jpg)
Find Records (with criteria)
010NODE.JSPART3 41
![Page 42: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/42.jpg)
Insert / Find Records (with criteria)
010NODE.JSPART3 42
![Page 43: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/43.jpg)
Find all Records
010NODE.JSPART3 43
![Page 44: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/44.jpg)
Delete / Remove a Record
010NODE.JSPART3 44
![Page 45: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/45.jpg)
Update a Record
010NODE.JSPART3 45
![Page 46: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/46.jpg)
MongoDB& Node.js OBJECT-DOCUMENT MODEL
![Page 47: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/47.jpg)
The MongoDB module supports all kinds of queries over the data ◦ Creating new documents ◦ And adding records
◦ Editing existing documents ◦ And their records
◦ Removing documents and records ◦ Querying whole documents or parts of them
MongoDB Queries
010NODE.JSPART3 47
![Page 48: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/48.jpg)
Once installed, MongoDB must be started before it can be used ◦ Go to installation folder and run mongod
$cdpath/to/mondodb/installation/folder$mongod
◦ Or add mongod.exe to the PATH
When run, MongoDB can be used from Node.js
Using MongoDB with Node.js
48
![Page 49: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/49.jpg)
Using MongoDB with Node.js Download MongoDB from the official web site: ◦ https://www.mongodb.org/downloads ◦ Installers for all major platforms
When installed, MongoDB needs a driver to be usable with a specific platform ◦ One to use with Node.js, another to use with .NET, etc…
Installing MongoDB dependency module for Node.js: $npminstallmongodb-g
010NODE.JSPART3 49
![Page 50: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/50.jpg)
◦ Create a server to host the database
◦ The 'mongodb' module is required varmongodb=require('mongodb');
varserver=newmongodb.Server('localhost',27017);
◦ Create mongodb client that connects to the server varmongoClient=newmongodb.MongoClient(server);
◦ Open connection to the mongodb server mongoClient.open(function(err,client){vardb=client.db('DATABASE_NAME');//queriesoverthedb});
MongoDB Module & Node.js
50
![Page 51: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/51.jpg)
Mongoose Overview OBJECT-DOCUMENT MODEL MODULE FOR NODE.JS
![Page 52: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/52.jpg)
Mongoose Overview Mongoose is a object-document model module in Node.js for MongoDB ◦ Wraps the functionality of the native MongoDB driver ◦ Exposes models to control the records in a doc ◦ Supports validation on save ◦ Extends the native queries
010NODE.JSPART3 52
![Page 53: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/53.jpg)
Run the following from the CMD/Terminal $npminstallmongoose
varmongoose=require('mongoose');
mongoose.connect(mongoDbPath);
varUnit=mongoose.model('Unit',{type:String});newUnit({type:'warrior'}).save(callback);//createUnit.find({type:'warrior'}).exec(callback);//fetch
53
Installing Mongoose
Load the Module
Connect to the Database
Create Models and persist data
![Page 54: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/54.jpg)
MongooseModels OBJECT-DOCUMENT MODEL SCHEMA
![Page 55: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/55.jpg)
® Mongoose supports Models ◦ i.e. fixed types of documents
® Used like object Constructors ◦ Needs a ‘mongoose.Schema’
55
Mongoose Models
![Page 56: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/56.jpg)
® Each of the properties must have a type ◦ Types can be Number, String, Boolean, array, object ◦ Even nested objects
56
Mongoose Models
![Page 57: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/57.jpg)
CRUD with Mongoose OBJECT-DOCUMENT MODEL SCHEMA
![Page 58: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/58.jpg)
CRUD with Mongoose ® Mongoose supports all the CRUD operations: ◦ Create –> modelObj.save(callback) ◦ Read –> Model.find().exec(callback) ◦ Update –> modelObj.update(props, callback)" –> Model.update(condition, props, callback) ◦ Remove –> modelObj.remove(callback)" –> Model.remove(condition, props, callback)
010NODE.JSPART3 58
![Page 59: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/59.jpg)
Donationweb CASE STUDY EXAMPLE
![Page 60: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/60.jpg)
Donation: Resource, URIs & Methods
{…}=variablevalue;changeablebyuser/applicaPontorefertospecificresource
Resource URI(structure) HTTPRequest
ListofDonaPons /donaPons GET
GetaSingleDonaPon /donaPons/{id} GET
UpvoteaDonaPon /donaPons/{id}/votes PUT
DeleteaDonaPon /donaPons/{id} DELETE
UpdateaDonaPon /donaPons/{id} PUT
AddaDonaPon /donaPons/{id} POST
We’ll look at this Use Case as an example… !
010NODE.JSPART3 60
![Page 61: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/61.jpg)
Creating the Model – Server Side
010NODE.JSPART3 61
![Page 62: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/62.jpg)
Creating the Routes (1) – Server Side
N.B.on‘imports’
010NODE.JSPART3 62
![Page 63: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/63.jpg)
Creating the Routes (2) – Server Side
010NODE.JSPART3 63
![Page 64: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/64.jpg)
Creating the Routes (3) – Server Side
010NODE.JSPART3 64
![Page 65: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/65.jpg)
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license
Attribution: this work may contain portions from ◦ "Databases" course by Telerik Academy under CC-BY-NC-SA license and
010NODE.JSPART3 65
![Page 66: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/66.jpg)
Great Resources Official Tutorial – https://nodejs.org/documentation/tutorials/ Official API – https://nodejs.org/api/ Developer Guide – https://nodejs.org/documentation Video Tutorials – http://nodetuts.com Video Introduction – https://www.youtube.com/watch?v=FqMIyTH9wSg YouTube Channel – https://www.youtube.com/channel/UCvhIsEIBIfWSn_Fod8FuuGg Articles, explanations, tutorials – https://nodejs.org/community/
010NODE.JSPART3 66
![Page 67: Web Application Development · MongoDB Overview § Installation § Structure and documents ... Executing queries on MongoDB data 3. Mongoose Overview § Mongoose Models § Types of](https://reader033.vdocuments.site/reader033/viewer/2022042223/5eca58cac38f4e40c93ea108/html5/thumbnails/67.jpg)
Questions?
010NODE.JSPART3 67