COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB...

21
COMP 430 Intro. to Database Systems MongoDB

Transcript of COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB...

Page 1: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

COMP 430Intro. to Database Systems

MongoDB

Page 2: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

What is MongoDB?

“Humongous” DB

• NoSQL, no schemas DB

• Lots of similarities with SQL RDBMs, but with more flexibility

• No joins because data is denormalized & pre-joined

• Open source

Page 3: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Collections of documents{_id: “S1234”,first_name: “John”,last_name: “Smith”,major: “ECON”

}

{_id: “S5678”,first_name: “Mary”,last_name: “Jones”,major: “COMP”,minor: “STAT”

}

Collection = tableDocument = record

Every document has unique _id, even if not user-defined.

Documents need not have the same

fields.

Page 4: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Relating data via embedding

{_id: “S1234”,first_name: “John”,last_name: “Smith”,major: “ECON”,address: {

street: “1514 Nowhere St.”,city: “Houston”,state: “TX”

}}

Data all stored together for locality.

Denormalized.Possible redundancy.

Page 5: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Relating data via embedding

{_id: “S1234”,first_name: “John”,last_name: “Smith”,major: “ECON”,addresses: [{

street: “1514 Nowhere St.”,city: “Houston”,state: “TX”},{street: “7729 Somewhere St.”,city: “Kansas City”,state: “KS”}

]}

Page 6: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Relating data via references

{_id: 34237first_name: “John”,last_name: “Smith”

}

{title: “A book about books”,author: 34237

}

{title: “A little bookish book”,author: 34237

}

{title: “A new book”,author: 34237

}

_id field = primary keyReference = foreign key

Page 7: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Relating data via references

{first_name: “John”,last_name: “Smith”,books: [103, 279, 541]

}

{_id: 103,title: “A book about books”

}

{_id: 279,title: “A little bookish book”

}

{_id: 541,title: “A new book”

}

_id field = primary keyReference = foreign key

Page 8: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Relating data via embedding + references

{first_name: “John”,last_name: “Smith”books: [103, 279, 541]

}

{first_name: “Mary”,last_name: “Jones”,books: [103, 541]

}

{_id: 103,title: “A book about books”

}

{_id: 279,title: “A little bookish book”

}

{_id: 541,title: “A new book”

}

Denormalized.Many-to-many

junction info put in one document.

Page 9: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Embedding & relating replaces joins

No need for joins, because data is already explicitly linked.

This gives up SQL’s flexibility of joining on conditions other that PK=FK.

Focus on the common case.

Page 10: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

DB/application interface

Intention is for DB & application to have equivalent data representation.

• Explicit references

• Built-in lists and associative arrays

• Flexible field types

• Flexible field membership

Page 11: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Storage model

Emphasize data locality:

• Each document stored as one unit, even with embedding

• Each collection stored as one unit.

Uses BSON, a binary-encoded variant of JSON key/value pairs

Page 12: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

CRUD operationsSQL-like, but with a library

Page 13: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Filtering

db.students.find({major: “COMP”})

db.students.find({major: “COMP”, minor: “STAT”})

db.students.find({$or: [{major: “COMP”}, {major: “ELEC”}]})

db.students.find({major: {$in: [“COMP”, “ELEC”]}})

db.students.find({gpa: {$gt: 3.8}})

db.students.find({address.street: {$exists: true}})

Collection name

Syntax structured as key/value pairs with

some special $keywords.

Embedded field name

Page 14: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Selecting fields

db.students.find({major: “COMP”}, {first_name: true, last_name: true, major: false})

Filter Specifies desired fields.

Page 15: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

db.students.insert({first_name: “Susan”, last_name: “Walsh”})

db.students.update({first_name: “John”, last_name: “Smith”},{$set: {major: “HIST”}},{multi: true,upsert: true})

db.students.update({first_name: “John”, last_name: “Smith”},{first_name: “Jonathan”, last_name: “Smythe”})

db.students.update({major: “COMP”},{$unset: {minor: “”}})

Filter

Change multiple documents?

Insert if no match found?

Delete field.

Change entire document.

Inserting & updating

Change only this.

Page 16: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Deleting

db.students.remove({major: “COMP”})

Page 17: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Transactions

db.students.update(…, $isolated: true)

db.students.remove(…, $isolated: true)

By default, each write to a document is atomic.

Make entire collections’ writes atomic.

Not intended for transaction-heavy systems.

Page 18: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Some other features

Page 19: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Grouping & aggregation

db.students.aggregate({$match: …},{$group: …},{$sort: …})

db.students.group(…)

db.students.mapReduce(map_fn, reduce_fn)

db.students.distinct(“major”)

Page 20: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

B-tree indices

db.students.ensureIndex({last_name: 1, gpa: -1})

Creates index that first orders last_nameascending, then gpa decending.

Automatically create index on _id.

Page 21: COMP 430 Intro. to Database Systems · What is MongoDB? “Humongous” D •NoSQL, no schemas DB •Lots of similarities with SQL RDBMs, but with more flexibility •No joins because

Data distribution

Supports replication & sharding (partitioning)