59
Data Models Past, Present, Future Raj Sunderraman Department of Computer Science Georgia State University June 2012

New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

Data  Models  Past,  Present,  Future  

Raj  Sunderraman  Department  of  Computer  Science  

Georgia  State  University  June  2012  

Page 2: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

Data  Models  

1960s:      File  systems,  Access  Methods    Hierarchical  (IMS)  and  Network  Data  Models  (IDMS)  

1970s:    RelaLonal  Data  Model  (tables,  SQL)    ER  Conceptual  Data  Model  

1980s:    Object-­‐Oriented  Data  Model  (ODL/OQL)  

1990s:    XML  Data  Model  (XPath,  XQuery,  XML  Schema)  

Late  2000s:      NoSQL  (nosql-­‐database.org)      Google  BigTable,  Amazon  Dynamo,  Cassandra,        MongoDB,  CouchDB  

Page 3: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

1960s  –  File  Systems    IBM  –  ISAM  (Indexed  SequenLal),  VSAM  (Virtual  Storage)    

Page 4: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models3

1. Hierarchical Model

root segment

dependentsegment type

COURSE# TITLE DESCRIPTION

STUD# NAME GRADE

DATE LOCATION FORMATCOURSE# TITLE

STAFF# NAME

COURSE

PREREQ OFFERING

TEACHER STUDENT

Fig. 1. PDBR (physical database record) type for the education database (schema)

IMS (Information Management System) Data Model (IBM product) Ref: CJ Date’s book

Page 5: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models4

M23 Dynamics ......

750106 Oslo F2

741104 Dublin F3730813 Madrid F3

M19 Calculus

M16 Trigonometry

761620 Tallis, T. B

183009 Gibbons, O. A

102141 Byrd, W. B

421633 Sharp, R.

OFFERINGOFFERING

OFFERING

STUDENT

STUDENT

STUDENT

TEACHER

PREREQ

PREREQ

COURSE

Fig. 2. Sample PDBR occurrence for the education database (database instance)

IMS (Information Management System) Data Model (cont.)

Note: IMS is a hierarchical database model. It is similar to (but not exactly the same as) the XML data model.

Page 6: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models5

Many-to-many (m:m) relationships

• Many-to-many relationships in hierarchical structure will contain redundant data

• IMS removes redundant data using logical parent pointers

Page 7: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models6

Many-to-many Relationships using logical parent pointers

AREA# ANAME ADESCN

REMARKS SNAME BDESCNBNAME DATE

AREA

SIGHTING

Fig. 3. Required record structure for the survey database (schema).BNAME – name of a bird. SNAME – scientific name of the bird.

BNAME SNAME BDESCN

BIRD

Fig. 4. Record structure of the bird database

Note: The same type of birds may appear in different areas, so the relationship between AREA and SIGHTING is a m:m relationship. The SNAME and BDESCN of a bird BNAME will be replicated under different areas.

To remove redundant data, we first create another database to store the information of birds as shown below:

Page 8: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models7

Fig. 5. Area and BIRD PDBs.

Many-to-many Relationships using logical parent pointers (cont.)

BNAME SNAME BDESCN

BIRD

AREA# ANAME ADESCN

AREA

SIGHTING

REMARKSDATE

Logical parent pointer

Note: DATE and REMARKS depend on AREA and SIGHTING

We then redesign the schema in Fig 3 to the below using logical parent pointers.

Note: Logical parent pointer is similar to XML IDREF.

similar to XML IDREF

Page 9: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models11

2. Network Model

D1

E4

E13

DEPARTMENT

DEPT

EMP

EMP

D2

E10

E22

DEPARTMENT

DEPT

E31

E33

EMP

EMP

EMP

EMP

D3

E3

E12

DEPARTMENT

DEPT

E16

EMP

EMP

EMP

Fig. 9. A Department-employee database instance.

Note: Each employee only works for one department and a department may have many employees. The relationship between employee and department is a many-to-one relationships.

Network Model was proposed by DBTG (Database Task Group) in 1971.

Ref: CJ Dates’ book. Software Product: IDMS

pointer

Page 10: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models12

DEPT

DEPTEMP

EMP

owner (record type)

member (record type)

set name

DEPT

EMP

DEPTEMP

Fig. 10. Structure of the set DEPTEMP.Each employee works for only one department

Network Model Data Structure (schema diagram)

(simplified schema diagram)

or

Note: A set is a 1:m relationship from owner to member. E.g. a dept has many employees and each employee only works for one dept.

Page 11: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models13

A three level network example

D1

E4

E13

DEPTMEP

DEPT

EMP

EMP

V1

D2

DEPTMEP

E10

E22

DEPT

EMP

EMP

DIV

D3

E3

E12

DEPTMEP

DEPT

EMP

EMP

V2

DIV

DIVDEPT DIVDEPT

Fig. 11. A division-department-employee database instance.Each department belongs to one Division. There are 2 divisions.

a record type may be both a owner and member of two set types

Page 12: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models14

Fig. 12. Structure of the sets DIVDEPT and DEPTEMP

A three level network example (cont.)

A record type may be both a owner and member of two set types

DIV

DIVDEPT

EMP

DEPT

DEPTEMP

Page 13: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models15

One owner with two members

J44

J46

EMPJOB

JOB

JOB

E22

L02

EMPCOURSE

M16

M19

COURSE

EMP

COURSE

COURSE

Fig. 13. An employee-history database instance

Page 14: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models16

Fig. 14. Structure of the sets EMPJOB and EMPCOURSE

One owner with two members (cont.)

EMP

EMPJOB

JOB

EMPCOURSE

COURSE

Page 15: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models17

Many-to-many relationship E.g. part and supplier relationship.

Note: Network model cannot represent m:m relationships directly. A m:m relationship can be simulated by two 1:m relationships and a dummy record type (e.g. SP in Fig 15). Not a very nice solution!

Fig. 15. Structure of the sets S-SP and P-SP.

SP

P-SP

P

S-SP

S

Page 16: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

CS4221: Hierarchical and Network Models18

Many-to-many relationship (cont.)

s2 Jones 10 Paris s4 Clark 20 London

P-SP

S-SP

P1 Nut Red 12 London P2 Bolt Green 17 Paris P4 Screw Red 14 London P5 Cam Blue 12 Paris

s2 P1 300 s2 P2 400 s4 P2 200 s4 P4 300 s4 P5 400

S-SP

P-SP P-SP P-SPp p p p

SP SP SP SP SP

Fig. 16. A suppliers-and-parts database instance

Q: How to represent 1:1 relationships? N-ary relationships?

Page 17: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Page 18: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Page 19: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

Copyright © 2011 Ramez Elmasri and Shamkant Navathe

Page 20: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

SQL  Queries    Find  names  of  employees  who  work  on  every  project  controlled  by  department  5.    select lname,fname from employee where not exists ( select * from project b where dnum=5 and not exists ( select * from works_on c where ssn = c.essn and b.pno = c.pno));          For  each  project  on  which  more  than  two  employees  work,  retrieve  the  project  number,  the  project  name,  and  the  number  of  employees  who  work  on  the  project.     select pnumber,pname,count(*) from project,works_on where pnumber=pno group by pnumber,pname having count(*)>2;  

Page 21: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

3-­‐Layered  Architecture  

Page 22: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

EnLty  RelaLonship  Model  

Page 23: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� G C Z � � G[ � � \ [

6M K ] � 6 4 �* �M ! �! K! #! $ �� � # � ] % ) ( � -! � �! # ,! % ,� $ % ) ( � .

6M T � 6 4 �* � M �� * % " � �" ) # T ! # $ (! $ � -� * + �� ! , �� * % " � �" ) # '! # $ (! $ � .

6 ^T � 6 4 �* � ^ ( �% � T ! # $ (! $ � - I ( �% " �� ! # 6 6 ,! �! ! � � <" � + ! #

6M T � * + �� ! & � " � " '! % �) � ^T .

_

Page 24: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� G[

6M T 5 '! � � �� -* ! ' ' � , " # � �% 0! * �� . �* ) # �! " # , � 0" #" �" ) # � 0) %

? ! � � % " ( � ��

? % � '! �" ) # � +" � �

? � � � +) ,�

5) # � " , �% ,! �! ! � � ! ) ( � � ) 7" �� & � �! % � ! # , � � ( ," ) � 2

K) 7" �� +! 7 � � �! % � ! * �" # $ " # � + �� 2

� �! % � � ! � ! * � " # ) # � ) % � ) % � � ) 7" �� 2

� � ( ," ) � � % ) , (* � ) # � ) % � ) % � � ) 7" �� 2

S! * + � ) 7" � " � � % ) , (* � , � ) # � � � ( ," ) 2

`

Page 25: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� G[ a� E �b c �� C �d �� � � � � �

ef ghi j kl h m no eh p

k g gi e qr gh s gi ef t g e gu h v

k g gi e qr gh ef gh thi wxh ki v

k g gi e qr gh ef gh thi u h f t gy v

k g gi e qr gh h f rz { euz pl nu ni | q u kl} ~ f �� y e gh � j euz � w� h vi h u k g e n f sy e� �h g� � g ki � s g ki s ef o hi sh � g ki � � s g ki i h �� f vi h u k g e n f sy e� � gr � e n n� f h �� w ef o hi sh � gr � e n � � n� f s v

� vef ghi j kl h � g ki p

k g gi e qr gh s gi ef t f kz h v

k g gi e qr gh � gi r l g ~ � �i p s gi ef t s gi h h g | s gi ef t l e g w � k � �i h s s vi h u k g e n f sy e� �h g� m no eh � s g ki i h �� f ef o h i sh m n o eh � � s g ki s v

� vef ghi j kl h � gr � e n p

k g gi e qr gh s gi ef t f kz h v

k g gi e qr gh s gi ef k � �i h s s vi h u k g e n f sy e� �h g� m no eh � n � f s ef o hi sh m no eh � � n� f h �� w v

� v

Page 26: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� \ [ ¢� � � � � � �

6 ^T ^ ( �% " �� �

-/ . X " # , � + � � �! % ) 0 � ) 7" � N ]) # � � " � + � + � � " # , N

� � ' �* � � 2 � �! %

0 % ) � K) 7" �� �

< + �% � � 2 �" � ' �U N ]) # � � " � + � + � � " # , N

O �

Page 27: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� \ [ ¢� � � � � � �

� � ' �* � ? 0 % ) � ? < + �% � � �! � �� � # � " # 6 ^T " � * ) # � � % (* � � , ! � 0) ' ') < � �

� S T S 5 � 3 � � <) % , 0) ' ') < � , � ! '" � � ) 0 �R � % �� � " ) # � - ( � " # $ * ) # � �! # � � ! # ,

7! % " ! ' �� , � 0" # � , " # � + � X W 6 K * '! ( � � .

X W 6 K 3 � � <) % , 0) ' ') < � , � ! '" � � ) 0 7! % " ! ' � , �* '! % ! �" ) # �

7! % " ! ' � " � , �* '! % � , �? $" 7" # $ ! # �R � % �� � " ) # < +) � � 7! ' ( � " � ! * ) ' ' �* �" ) # � �� � - � �� " * ! ' ' �

! # �R � � # � =* ) ( ' , � ! #) � + �% � � ' �* � ? 0 % ) � ? < + �% � .? 0) ' ') < � , � ! # ) � �" ) #! ' L � 3 � � <) % ,

? 0) ' ') < � , � � + � #! � � ) 0 � + � 7! % " ! ' �

� Q S W S 3 � � <) % , 0) ' ') < � , � ! ) ) ' �! # ? 7! ' ( � , �R � % �� � " ) # -* ! # ( � � ) # ' �

* ) # � �! # � � ! # , 7! % " ! ' �� , �* '! % � , " # � + � X W 6 K * '! ( � � . =

� + � 6 ^T I ( �% � � % ) , (* �� ! ! $ ) 0 ) 4 �* � � 2

O �

Page 28: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� \ [ ¢� � � � � � �

-1 . X " # , � + � #! � �� ) 0 � + � � �! % � ) 0 N 5! � ! '! #* ! N

� � ' �* � � 2 #! � �

0 % ) � K) 7" �� � & � 2� �! % � �

< + �% � � 2 �" � ' �U N 5! � ! '! #* ! N

- � . S '" � " #! �" # $ , ( � '" * ! � �� - ," � �" #* � 3 � � <) % , .

X " # , � + � #! � �� ) 0 � �! % � ) 0 NM " � # � � N � ) 7" ��

� � ' �* � ," � �" #* � � 2 #! � �

0 % ) � K) 7" �� � & � 2� �! % � �

< + �% � � 2) < # � , � � 2 #! � �U NM " � # � � N

O �

Page 29: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� \ [ ¢� � � � � � �

5) � � ' �R ) ( � � ( � � �� � �

-� . ] � � � � � ) 0 � ! " % � ) 0 � �! % � '" 7" # $ ! � � + � � ! � � ! , , % �� �

� � ' �* � ," � �" #* � � � % (* � -� �! % / � � / & � �! % 1 � � 1 .

0 % ) � � �! % � � / & � �! % � � 1

< + �% � � / 2! , , % U � 1 2! , , % ! # , � / 2 #! � � > � 1 2 #! � �

� + � % �� ( ' � � �� � ) 0 � +" � I ( �% � " �

� � � > � � % (* � J 8� �! % / � � �! % & � �! % 1 � � �! % ;�

J ) � � � � (* + ! � �� � * ! # #) � ! � � �! % " # ! # 6M T , �* '! % ! �" ) # �

� +) % �* ( � � � � ' �* � � �! % / � � / & � �! % 1 � � 1

O �

Page 30: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� \ [ ¢� � � � � � �

� ( I ( �% " �� �

- 9 . ] � � � + � � �! % � " # � ) 7" �� � ! , � �M " � # � � 2

� � ' �* � �

0 % ) � K) 7" �� �

< + �% � � 2) < # � , � � 2 #! � �U NM " � # � � N

$" 7 �� ( � � + �M " � # � � K) 7" �� 2 � +" � * ! # � ( � � , " # � + � 0 % ) � * '! ( � � ! �

0) ' ') < � �

� � ' �* � ," � �" #* � � 2 #! � �

0 % ) � -� � ' �* � �

0 % ) � K) 7" �� �

< + �% � � 2) < # � , � � 2 #! � �U NM " � # � � N . , & , 2� �! % � �

Y �

Page 31: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� \ [ ¢� � � � � � �

6% , �% " # $ � + � W �� ( ' � �

-¥ . ] � � M " � # � � � ) 7" �� ) % , �% � , � ' � # $ � + - �" �� % ) 3 � # � �" � ' � .

� � ' �* � �

0 % ) � K) 7" �� �

< + �% � � 2) < # � , � � 2 #! � �U NM " � # � � N

) % , �% � � 2 ' � # $ � + & � 2 �" � ' �

? ! � * ) % , �� * � ! � � � � � 7* " 0" � , ! 0 � �% ) % , �% � - , � 0! ( ' � " � ! � * .Y A

Page 32: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

� \ [ ¢� � � � � � �

^ (! # �" 0" �% S R � % �� � " ) # � �

0) % ! ' ' R " # � � 5 -R .

�R " � � � R " # � � 5 -R .

- : . ] � � � �! % � ! * �" # $ " # M " � # � � � ) 7" ��

� � ' �* � �

0 % ) � � �! % � �

< + �% � �R " � � � � " # � 2� �! % % � ,H # � � 2) < # � , � � 2 #! � �U NM " � # � � N

-¦ . ] � � � �! % � < +) ! � � �! % ) # ' � " # M " � # � � � ) 7" ��

� � ' �* � �

0 % ) � � �! % � �

< + �% � 0) % ! ' ' � " # � 2� �! % % � ,H # � � 2) < # � , � � 2 #! � �U NM " � # � � N

Y O

Page 33: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

XML  Data  Model    

Semi-­‐Structured  Data  Model  Tree  Structure  Querying  via  Xpath  and  Xquery    Go  to  h`p://Lnman.cs.gsu.edu/~raj/elna-­‐lab-­‐2010  for  examples  

Page 34: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

Late  2000s  -­‐  NoSQL  

•  h`p://nosql-­‐database.org/  •  Non-­‐relaLonal  •  Distributed  •  Open  source  •  Horizontally  scalable  •  Schema-­‐free  

•  Simple  API  •  Eventually  consistent  (BASE  not  ACID)  •  Big  Data  

Page 35: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

MongoDB    

•  A  Mongo  System  holds  a  set  of  databases  •  A  database  holds  a  set  of  collecLons  •  A  collecLon  holds  a  set  of  documents  •  A  document  is  a  set  of  fields  •  A  field  is  a  key-­‐value  pair  •  A  key  is  a  name  (string)  •  A  value  is  a    

–  Basic  type  (string,  integer…)  –  A  document  –  An  array  of  values  

Page 36: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> show dbs admin harmony-development harmony-test local ...> use harmony-development switched to db harmony-development> show collections accounts activities assets items ...

Page 37: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.accountsharmony-development.accounts

> db.accounts.count()1

> db.accounts.find().forEach(function(doc) { print(tojson(doc));});

Page 38: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

{ "_id" : ObjectId("4be97eaebcd1b30e86000003"), "title" : "Ordered List", "creator_id" : ObjectId("4be97eadbcd1b30e86000001"), "memberships" : [ ObjectId("4be97eadbcd1b30e86000001"), ObjectId("4be97eaebcd1b30e86000002") ]}

Page 39: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> use testingswitched to db testing

> db.colors.insert({name:'red', primary:true})> db.colors.insert({name:'green', primary:true})> db.colors.insert({name:'blue', primary:true}) > db.colors.insert({name:'purple', primary:false})> db.colors.insert({name:'orange', primary:false})> db.colors.insert({name:'yellow', primary:false})

Page 40: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> var cursor = db.colors.find()> cursor.next(){ "_id" : ObjectId("4bed7aeb0b4acd070c593ba6"), "name" : "red", "primary" : true}

Page 41: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> cursor{ "_id" : ObjectId("4bed7af40b4acd070c593ba7"), "name" : "green", "primary" : true }{ "_id" : ObjectId("4bed7af80b4acd070c593ba8"), "name" : "blue", "primary" : true }{ "_id" : ObjectId("4bed7b570b4acd070c593ba9"), "name" : "purple", "primary" : false }{ "_id" : ObjectId("4bed7b6a0b4acd070c593baa"), "name" : "orange", "primary" : false }{ "_id" : ObjectId("4bed7b7d0b4acd070c593bab"), "name" : "yellow", "primary" : false }

Page 42: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.colors.find({name:'green'}){ "_id" : ObjectId("4bed7af40b4acd070c593ba7"), "name" : "green", "primary" : true }

SELECT * from colors WHERE name = 'green'

Page 43: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

SELECT name from colors WHERE primary = 1

> db.colors.find({primary:true}, {name:true}){ "_id" : ObjectId("4bed7aeb0b4acd070c593ba6"), "name" : "red" }{ "_id" : ObjectId("4bed7af40b4acd070c593ba7"), "name" : "green" }{ "_id" : ObjectId("4bed7af80b4acd070c593ba8"), "name" : "blue" }

Page 44: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.colors.find({name:/l/}){ "_id" : ObjectId("4bed7af80b4acd070c593ba8"), "name" : "blue", "primary" : true }{ "_id" : ObjectId("4bed7b570b4acd070c593ba9"), "name" : "purple", "primary" : false }{ "_id" : ObjectId("4bed7b7d0b4acd070c593bab"), "name" : "yellow", "primary" : false }

Page 45: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.colors.find({primary:true}).sort({name:1}).limit(1){ "_id" : ObjectId("4bed7af80b4acd070c593ba8"), "name" : "blue", "primary" : true }

> db.colors.find({primary:true}).sort({name:-1}).limit(1) { "_id" : ObjectId("4bed7aeb0b4acd070c593ba6"), "name" : "red", "primary" : true }

> db.colors.find({primary:true}).sort({name:1}).skip(1).limit(1){ "_id" : ObjectId("4bed7af40b4acd070c593ba7"), "name" : "green", "primary" : true }

Page 46: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.people.insert({name:'John', age:28})> db.people.insert({name:'Steve', age:29})> db.people.insert({name:'Steph', age:27})

Page 47: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.people.find({age: {$gt: 27}}){ "_id" : ObjectId("4bed80b20b4acd070c593bac"), "name" : "John", "age" : 28 }{ "_id" : ObjectId("4bed80bb0b4acd070c593bad"), "name" : "Steve", "age" : 29 }

SELECT * from people WHERE age > 27

Page 48: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

SELECT * from people WHERE age <= 27

> db.people.find({age: {$lte: 27}}){ "_id" : ObjectId("4bed80c10b4acd070c593bae"), "name" : "Steph", "age" : 27 }

Page 49: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

$gt$gte$lt$lte$ne$in$nin$mod

$all$size$exists$type$elemMatch$not$where

Page 50: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

Aggregationhttp://www.mongodb.org/display/DOCS/Aggregation

Page 51: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.colors.count() 6> db.colors.count({primary:true})3

Page 52: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.colors.distinct('name')[ "blue", "green", "orange", "purple", "red", "yellow" ]

> db.people.distinct('name', {age:28})[ "John" ]

Page 53: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.items.insert({title:'Home', template:'home'}) > db.items.insert({title:'What We Do', template:'page'}) > db.items.insert({title:'Our Writing', template:'page'})> db.items.insert({title:'Who We Are', template:'page'}) > db.items.insert({title:'Hire Us', template:'page'})

> var key = {template: true};> var initial = {count:0};> var reduce = function(obj, prev) { prev.count += 1; };

> db.items.group({key:key, initial:initial, reduce:reduce})[ {"template" : "home", "count" : 1}, {"template" : "page", "count" : 4}]

Page 54: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db.items.insert({tags: ['dog', 'cat']})> db.items.insert({tags: ['dog']}) > db.items.insert({tags: ['dog', 'mouse']})> db.items.insert({tags: ['dog', 'mouse', 'hippo']})> db.items.insert({tags: ['dog', 'mouse', 'hippo']})> db.items.insert({tags: ['dog', 'hippo']})

Page 55: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> var map = function() { this.tags.forEach(function(t) { emit(t, {count: 1}); }); }

Page 56: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> var reduce = function(key, values) { var count = 0; for(var i=0, len=values.length; i<len; i++) { count += values[i].count; } return {count: count}; }

Page 57: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> var result = db.items.mapReduce(map, reduce);

Page 58: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> var result = db.items.mapReduce(map, reduce); > result{ "ok" : 1, "timeMillis" : 86, "result" : "tmp.mr.mapreduce_1273861517_683", "counts" : { "input" : 6, "emit" : 13, "output" : 4 }}

Page 59: New DataModels$ Past,$Present,$Future$tinman.cs.gsu.edu/~raj/8711/sp19/mongo.pdf · 2019. 3. 1. · CS4221: Hierarchical and Network Models. 6. Many-to-many Relationships using logical

> db[result.result].find(){ "_id" : "cat", "value" : { "count" : 1 } }{ "_id" : "dog", "value" : { "count" : 6 } }{ "_id" : "hippo", "value" : { "count" : 3 } }{ "_id" : "mouse", "value" : { "count" : 3 } }