MongoDB Databases, Collections, Documents Dr Janusz R. Getta - - PowerPoint PPT Presentation

mongodb databases collections documents
SMART_READER_LITE
LIVE PREVIEW

MongoDB Databases, Collections, Documents Dr Janusz R. Getta - - PowerPoint PPT Presentation

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... CSCI235 Database Systems MongoDB Databases, Collections, Documents Dr Janusz R. Getta School of Computing


slide-1
SLIDE 1

CSCI235 Database Systems

MongoDB Databases, Collections, Documents

Dr Janusz R. Getta

School of Computing and Information Technology - University of Wollongong

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 1 of 34 19/9/20, 8:17 pm

slide-2
SLIDE 2

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 2/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 2 of 34 19/9/20, 8:17 pm

slide-3
SLIDE 3

Basics

MongoDB is a database system that belong to a class of so called NoSQL database systems based on a data model different from the relational model and data definition, manipulation, retrievel, and administration languages different from SQL MongoDB data model (BSON) is based a on concept of key:value pairs grouped into documents and arrays MongoDB database system operates on a number of databases A MongoDB database is a set of collections A MongoDB collection is a set of documents A MongoDB document is a set of key:value pairs A MongoDB value is either an atomic value or a document or an array A MongoDB atomic value is of one of the types included BSON specification like number, string, date, etc A MongoDB array is a sequence of values

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 3/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 3 of 34 19/9/20, 8:17 pm

slide-4
SLIDE 4

Basics

Each MongoDB key:value pair must have a unique key within in a document Each MongoDB document must have a unique identifier within a collection Each MongoDB collection must have a unique name within a database

{"_id": ObjectId(), "full name": {"first name":"James", "initials":null, "last name":"Bond"}, "employee number":"007", "skills":["cooking", "painting", "gardening"], "cars owned":[ {"rego":"007-1", "made":"Porsche"}, {"rego":"007-2", "made":"Ferrari"} ], "secret codes":[ [1,2,3,4], [9,8,7,5] ], "date of birth":new Date("1960-01-01") }

A sample BSON document TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 4/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 4 of 34 19/9/20, 8:17 pm

slide-5
SLIDE 5

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 5/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 5 of 34 19/9/20, 8:17 pm

slide-6
SLIDE 6

Architecture

MongoDB flexible storage architecture automatically manages the movement of data between storage engine technologies using native replication MongoDB stores data as documents in a binary representation called BSON (Binary JSON) MongoDB query model is implemented as methods or functions within the API of a specific programming language, as opposed to a completely separate language like SQL MongoDB provides horizontal scale-out for databases on low cost, commodity hardware or cloud infrastructure using a technique called sharding, which is transparent to applications In-Memory storage engine enables performance advantages of in- memory computing for operational and real-time analytics workloads MongoDB Enterprise Advanced provides extensive capabilities to defend, detect, and control access to data (data security)

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 6/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 6 of 34 19/9/20, 8:17 pm

slide-7
SLIDE 7

Architecture

MongoDB Ops Manager makes easy for operations teams to deploy, monitor, backup and scale the system (system management) MongoDB Atlas provides all of the features of Database as a Service cloud computing model

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 7/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 7 of 34 19/9/20, 8:17 pm

slide-8
SLIDE 8

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 8/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 8 of 34 19/9/20, 8:17 pm

slide-9
SLIDE 9

Server

Starting MongoDB server with options --dbpath, --port, and

  • -bind_ip

Starting MongoDB server with options --dbpath, --port, and server running on a localhost

  • r simply ...

Starting MongoDB command based shell

mongod --dbpath data --port 4000 --bind_ip 10.0.2.100

Starting MongoDB server

mongod --dbpath data --port 4000 --bind_ip localhost

Starting MongoDB server

mongod --dbpath data --port 4000

Starting MongoDB server

mongo --port 4000

Starting MongoDB command client TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 9/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 9 of 34 19/9/20, 8:17 pm

slide-10
SLIDE 10

Server

Getting the first help from MongoDB shell

help

Getting MongoDB help

db.help() help on db methods show dbs show database names show collections show collections in current database use db_name set current database ... ... ... ... ... ...

MongoDB help messages TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 10/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 10 of 34 19/9/20, 8:17 pm

slide-11
SLIDE 11

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 11/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 11 of 34 19/9/20, 8:17 pm

slide-12
SLIDE 12

Databases

Setting a default database For example using a database local Creating and switching to a new database mydb Listing the databases

use database-name

Setting 'database-name' as a default database

use local

Setting 'local' as a default database

use mydb

Setting 'mydb' as a default databas

show dbs

Listing all databases

local 0.000GB mydb 0.000GB

Databases TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 12/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 12 of 34 19/9/20, 8:17 pm

slide-13
SLIDE 13

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 13/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 13 of 34 19/9/20, 8:17 pm

slide-14
SLIDE 14

Collections

Creating a new collection with an empty document Listing the contents of a collection Listing the collections

db.mycol.insert({})

Creating a new collection mycol

db.mycol.find()

Listing a collection mycol

{ "_id" : ObjectId("57e385f8ffc660a351b58010") } show collections

Listing the names of collections

mycol

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 14/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 14 of 34 19/9/20, 8:17 pm

slide-15
SLIDE 15

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 15/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 15 of 34 19/9/20, 8:17 pm

slide-16
SLIDE 16

Documents

Creating a new non empty document Listing the contents of a collection

db.mycol.insert({"one":"1", "many ones":[1,1,1,1]})

Inserting a document into a collection mycol

db.mycol.find()

MongoDB Shell

{ "_id" : ObjectId("57e385f8ffc660a351b58010") } { "_id" : ObjectId("57e38cbeffc660a351b58012"), "one" : "1", "many ones" : [ 1, 1, 1, 1 ] }

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 16/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 16 of 34 19/9/20, 8:17 pm

slide-17
SLIDE 17

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 17/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 17 of 34 19/9/20, 8:17 pm

slide-18
SLIDE 18

Formatting

Listing the nicely formatted contents of a collection

db.mycol.find().pretty()

Listing a nicely formatted collection

{ "_id" : ObjectId("57e385f8ffc660a351b58010") } { "_id" : ObjectId("57e38cbeffc660a351b58012"), "one" : "1", "many ones" : [ 1, 1, 1, 1 ] }

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 18/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 18 of 34 19/9/20, 8:17 pm

slide-19
SLIDE 19

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 19/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 19 of 34 19/9/20, 8:17 pm

slide-20
SLIDE 20

DDL

Removing all documents from a collection Removing a collection Removing a database

db.mycol.remove({})

Removing all documents from a collection

db.mycol.drop()

Dropping a collection

db.dropDatabase()

Dropping a database TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 20/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 20 of 34 19/9/20, 8:17 pm

slide-21
SLIDE 21

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 21/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 21 of 34 19/9/20, 8:17 pm

slide-22
SLIDE 22

DML

Let a file dbload.js contains the following insert methods Processing a script inserts two documents into a collection mycol Listing a collection mycol

db.mycol.insert({"CITY": {"name":"Wollongong", "population":"80K", "country":"Australia", "state":"New South Wales"} }); db.mycol.insert({"EMPLOYEE":{"enum":1234567, "full-name":"Janusz R.Getta", "salary": "200K", "hobbies":["cooking", "painting", "gardening"]} });

Inserting documents into a collection mycol

load("dbload.js")

Processing a script dbload.js

db.mycol.find().pretty()

Listing a nicely formatted collection mycol TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 22/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 22 of 34 19/9/20, 8:17 pm

slide-23
SLIDE 23

DML

{ "_id" : ObjectId("57e3c817fe6a1bfd5105022a"), "CITY" : { "name" : "Wollongong", "population" : "80K", "country" : "Australia", "state" : "New South Wales" } } { "_id" : ObjectId("57e3c817fe6a1bfd5105022b"), "EMPLOYEE" : { "enum" : 1234567, "full-name" : "Janusz R.Getta", "salary" : "200K", "hobbies" : [ "cooking", "painting", "gardening" ] } }

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 23/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 23 of 34 19/9/20, 8:17 pm

slide-24
SLIDE 24

MongoDB Databases, Collections, Documents

Outline

Basics Architecture Server Databases Collections Documents Formatting DDL DML Query Language

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 24/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 24 of 34 19/9/20, 8:17 pm

slide-25
SLIDE 25

Query language

MongoDB query language is based on a concept of pattern matching A query is expressed as a BSON pattern and all document from a collection that match the pattern are included in an answer A method find() is used to match a query pattern {"age":25}against the documents in a collection Matching of an empty pattern { } with a collection returns the returns an entire collection

db.department.find({"age":25})

Finding all documents that have a pair "age":25 at the topmost nesting level

db.department.find({})

Finding all documents in a collection TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 25/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 25 of 34 19/9/20, 8:17 pm

slide-26
SLIDE 26

Query language

The first document

db.department.insert( { "name":"School of Computing and Information Technology", "code":"SCIT", "total_staff_number":30, "budget":1000000, "address":{"street":"Northfields Ave", "bldg":3, "city":"Wollongong", "country":"Australia"}, "courses":[ {"code":"CSCI835", "title":"Database Systems", "credits":6}, {"code":"CSIT115", "title":"Data Management and Security", "credits":6}, {"code":"CSCI317", "title":"Database Performance Tuning", "credits":6}, {"code":"CSIT321", "title":"Software Project", "credits":12} ] } );

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 26/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 26 of 34 19/9/20, 8:17 pm

slide-27
SLIDE 27

Query language

The second document

db.department.insert( { "name":"School of Astronomy", "code":"SOA", "total_staff_number":5, "budget":10000, "address":{"street":"Franz Josef Str", "bldg":4, "city":"Vienna", "country":"Austria"}, "courses":[ {"code":"SOA101", "title":"Astronomy for Kids", "credits":3}, {"code":"SOA201", "title":"Black Holes", "credits":6}, {"code":"SOA301", "title":"Dark Matter", "credits":12} ] } );

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 27/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 27 of 34 19/9/20, 8:17 pm

slide-28
SLIDE 28

Query language

The third document

db.department.insert( { "name":"School of Physics", "code":"SOPH", "total_staff_number":25, "budget":100000, "address":{"street":"Victoria St", "bldg":25, "city":"Cambridge", "country":"UK"}, "courses":[ {"code":"SOPH101", "title":"Special Relativity", "credits":6}, {"code":"SOPH102", "title":"General Relativity", "credits":12}, {"code":"SOPH103", "title":"Quantum Mechanics", "credits":18} ] } );

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 28/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 28 of 34 19/9/20, 8:17 pm

slide-29
SLIDE 29

Query language

Find total number of documents in a collection Find all departments whose code is SCIT Find total number of departments whose code is SOPH Find all departments whose name is School of Physics and whose code is SOPH

db.department.count()

Counting the documents

db.department.find({"code":"SCIT"})

Finding the documents that match a given search pattern

db.department.find({"code":"SOPH"}).count()

Counting the documents that match a given search pattern

db.department.count({"code":"SOPH"}) db.department.find({"name":"School of Physics", "code":"SOPH"})

Finding the documents that match a given search pattern TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 29/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 29 of 34 19/9/20, 8:17 pm

slide-30
SLIDE 30

Query language

Comparison "key"="value" Comparison "key" > "value" Disjunction ("key1"="value1") or ("key2"="value2") Conjunction ("key1"="value1") and ("key2"="value2")

{"key":"value"}

Search pattern

{"key": {$eq:"value"}}

Search pattern

{"key": {$gt:"value"}}

Search pattern

{$or: [{"key1":"value1"},{"key2":"value2"}]}

Search pattern

{$and: [{"key1":"value1"},{"key2":"value2"}]}

Search pattern TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 30/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 30 of 34 19/9/20, 8:17 pm

slide-31
SLIDE 31

Query Language

Boolean expression (("key1"="value1") or ("key2"="value2")) and ("key3"="value3") Negation of a comparison "key" not ="value" Negation of an expression not (("key1"="value1") or ("key2"="value2")) Negation not ("key1"="value1")

{$and: [{$or: [{"key1":"value1"},{"key2":"value2"}]},{"key3"="value3"}]}

Search pattern

{"key": {$not: {$eq:"value"}}}

Search pattern

{$nor: [{"key1":"value1"},{"key2":"value2"}]}

Search pattern

{$nor: [{"key1":"value1"}]}

Search pattern TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 31/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 31 of 34 19/9/20, 8:17 pm

slide-32
SLIDE 32

Query language

A sample nested document

db.department.insert( { "name":"School of Computing and Information Technology", "code":"SCIT", "total_staff_number":30, "budget":1000000, "address":{"street":"Northfields Ave", "bldg":3, "city":"Wollongong", "country":"Australia"}, "courses":[ {"code":"CSCI835", "title":"Database Systems", "credits":6}, {"code":"CSIT115", "title":"Data Management and Security", "credits":6}, {"code":"CSCI317", "title":"Database Performance Tuning", "credits":6}, {"code":"CSIT321", "title":"Software Project", "credits":12} ] } );

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 32/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 32 of 34 19/9/20, 8:17 pm

slide-33
SLIDE 33

Query Language

Find all departments located in Wollongong Find all departments such that any offered course is worth less than 6 credits Find all departments that offer courses worth more than 12 and less than 18 credits Find all departments such that offer Quantum Mechanics course worth 6 credits

db.department.find({"address.city":"Wollongong"})

An access path

db.department.find({"courses.credits":{"$lt":6}})

An access path

db.department.find({"courses.credits": {"$gt":12, "$lt":18}})

An access path

db.department.find({"courses.credits":6,"courses.title":"Quantum Mechanics"})

Two access paths TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 33/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 33 of 34 19/9/20, 8:17 pm

slide-34
SLIDE 34

References

MongoDB Architecture, https://www.mongodb.com/mongodb- architecture Chodorow K. MongoDB The Definitive Guide, O'Reilly, 2013, Chapter 2

TOP Created by Janusz R. Getta, CSCI235 Database Systems, Spring 2020 34/34

MongoDB Databases, Collections, Documents file:///Users/jrg/235-2020-SPRING/SLIDES/WEEK08/19mongodbdbscolsdocs/19mongodbdbscolsdocs... 34 of 34 19/9/20, 8:17 pm