Top Banner
MongoDB CSC309 TA: Sukwon Oh
41

CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Apr 16, 2018

Download

Documents

vanthuan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

MongoDBCSC309 TA: Sukwon Oh

Page 2: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Review● SQL

○ “declarative” language for querying data○ tells what to find and not how to find

Page 3: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Review● RDBMS Characteristics

○ Easy to use○ Complicated to use it right○ Fixed schema○ Difficult to handle large amount of data

● Why?○ How the system works is hidden!

Page 4: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Review● NoSQL Philosophy (Not-Only SQL)

○ Simpler DB operations○ Complex query logic is moved to application code○ Relaxes ACID guarantees for better performance

Page 5: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Review● Types of NoSQL DB

○ Key-Value Store

○ Key-Data Structure Store

○ Key-Document Store

Page 6: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Why MongoDB?● To handle big amount of data● Better performance● Handle failures easily● Dynamic schema● Don’t need complex query operations● Your app is already using JSON● ...

Page 7: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Basics

Page 8: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Basics

Page 9: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Basics● Each DB has a set of collections● Each collection has a set of documents

○ Document looks like JSON format○ All documents have _id field with unique values

(primary key)● Each document contains a set of key-value

pairs

Page 10: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Sample Document{

“address”: {“building”: “1007”,“coord”: [-73.856077, 40.848447],“street”: “Morris Park Ave”,

...

Page 11: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

How to use MongoDB?● First install MongoDB by following

instructions at https://www.mongodb.org/downloads

● Install node.js MongoDB driver (or any other language you want to use)○ npm install mongodb

Page 12: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Node.js Driver● MongoDB API that you can use from

express.js

● Supports CRUD operations○ CRUD - create, read, update, delete

Page 13: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Connecting to DBvar client = require(‘mongodb’).MongoClient;client.connect(url, function(err, db) {

console.log(‘connected!’);db.close(); // closes connection

});

Page 14: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

MongoDB URI● Address of MongoDB instance you want to

connectmongodb://[username:password@]host[:port][/database][?options]● Example

○ ‘mongodb://localhost:27017/test’

Page 15: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Create● insertOne(), insertMany()

○ inserts 1 or multiple documents to a collection specified.

○ _id field is automatically generatedinsertOne Syntax:db.collection(‘collection’).insertOne(

{//document}, function(err, result){..});

Page 16: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

CreateinsertMany Syntax:db.collection(‘collection’).insertMany(

[{//doc1}, {//doc2},...], function(err, result){..});

Page 17: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Read● Use find() to query DB

○ All queries are performed on a single collection○ Use filters to select only interesting documents

■ Similar to WHERE clause in SQL○ Returns cursor object, which you can use to iterate

over query results■ Use cursor.each() to look at individual document

Page 18: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Filter● Filters are in following format{<field1>: <value1>, <field2>: <value2>..}● Examples

○ {“borough”: “Manhattan”}○ {“addresses.zipcode”: “10075”}○ {“ratings”: [5,8,9]}○ {“ratings.0”: 5}

Page 19: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Examplevar cursor = db.collection(‘restaurants’).find({“borough”: “Manhattan”});cursor.each(function(err, doc) {

console.log(doc);});

Page 20: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Query Operators● Filter can be complex by using following

operators○ $or○ $gt○ $lt○ $elemMatch○ $eq○ ...

Page 21: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Query Operators{<field1>: {<operator1>: <value1>}}

● Examples○ {“grades.score”: {$gt: 30}}○ {ratings: {$elemMatch: {$gt: 5, $lt: 9}}}

Page 22: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Query Operators● Logical AND

○ Write multiple filters separated by commas

● Example○ {“cuisine”: “Italian”, “address.zipcode”: “10075”}

Page 23: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Query Operators● Logical OR

○ Use $or

● Examples○ {$or: [ {“cuisine”: “Italian”}, {“address.zipcode”:

“10075”} ] }

Page 24: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Sorting● Use .sort() method after .find()

○ Similar to ORDER BY in SQL● Accepts a document with keys to sort by and

values as 1 for ascending order and -1 or descending order

● Example○ {“borough”: 1, “address.zipcode”: -1}

Page 25: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Cursor methods● We have used each() but other methods are

available○ skip(# doc to skip)○ sort()○ next()○ toArray()○ map()

Page 26: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Update● updateOne(), updateMany(), replaceOne()● Accepts 3 paramters

○ a filter○ update value○ options

● Cannot update _id field!

Page 27: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Update Operators● $set to change a field value● $currentDate to update a field value to

current date● ...

Page 28: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

UpdateupdateOne/updateMany Syntax:db.collection(‘collection’).updateOne/Many(

{//filter},{//update value},function(err, result) {..}

);

Page 29: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Exampledb.collection(‘restaurants’).updateMany(

{“address.zipcode”: “10016”, cuisine: “Other”},{

$set: {cuisine: “Category to be determined”},$currentDate: {“lastModified”: true}

},function(err, results) {

console.log(results);}

);

Page 30: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

replaceOne● replaceOne() will only preserve field values

that are updated and throw away existing fields that are untouched!

● Same syntax as insertOne()

Page 31: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

More on Update● By default, MongoDB does nothing when

none of documents are matched. However when upsert option is set to true, it will insert a new document if no matching documents are found.

Page 32: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Delete● deleteOne(), deleteMany()● Accepts a filter to choose which documents

to delete● Does not delete indexes or collections

○ Indexes are explained in next tutorial

Page 33: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

DeleteSyntax:db.collection(‘collection’).deleteMany(

{//filter}, function(err, result) {..});

Page 34: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Exampledb.collection(‘restaurants’).deleteOne(

{“borough”: “Queens”},function(err, results) {

console.log(results);}

);

Page 35: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Drop● To completely remove a collection, consider

using drop()● Many be more efficient than deleteMany({})Syntax:db.collection(‘collection’).drop(function(err, results) {..});

Page 36: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Aggregation● Operates on multiple documents● Similar to the popular MapReduce paradigm● Perform stage-based aggregation

○ Example■ Stage 1: Filter out uninteresting documents■ Stage 2: Group documents by some key■ Stage 3: Count # of documents with same key

Page 37: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

AggregationSyntax:db.collection.aggregate([<stage1>, <stage2>, ..])

Page 38: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Group Stage● Use $group to specify a stage{$group: {“_id”: “$key”, “field1”: {//accumulator}}

● Example○ {$group:{“_id”:”$borough”, “count”: {$sum: 1}}}

Page 39: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Accumulators● Only available in group stage and computes

values by combining documents with same key○ $sum○ $avg○ $max○ $min

Page 40: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Match Stage● Filters documents● Uses query syntax from before{$match: {//filter}}

Page 41: CSC309 TA: Sukwon Oh - University of Torontomashiyat/csc309/Tutorial/8/MongoDB.pdf · Review NoSQL Philosophy (Not-Only SQL) Simpler DB operations Complex query logic is moved to

Exampledb.collection(‘restaurants’).aggregate(

[{$match: {“borough”: “Queens”, “cuisine”: “Brazilian”}},{$group: {“_id”: “$address.zipcode”, “count”: {$sum: 1}}}

]).toArray(function(err, result) {

console.log(result);});