CC5212-1 Procesamiento Masivo de...

101
CC5212-1 PROCESAMIENTO MASIVO DE DATOS O TOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan Hogan [email protected]

Transcript of CC5212-1 Procesamiento Masivo de...

Page 1: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

CC5212-1PROCESAMIENTO MASIVO DE DATOS

OTOÑO 2018

Lecture 9NoSQL: MongoDB

Aidan Hogan

[email protected]

Page 2: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

NoSQL

Page 3: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

http://db-engines.com/en/ranking

Page 4: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

DOCUMENT STORES

Page 5: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Key–Value: a Distributed MapCountries

Primary Key Value

Afghanistan capital:Kabul,continent:Asia,pop:31108077#2011

… …

Tabular: Multi-dimensional Maps Countries

Primary Key capital continent pop-value pop-year

Afghanistan Kabul Asia 31108077 2011

… … … … …

Document: Value is a documentCountries

Primary Key Value

Afghanistan { cap: “Kabul”, con: “Asia”, pop: { val: 31108077, y: 2011 } }

… …

Page 6: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan
Page 7: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan
Page 8: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

http://cryto.net/~joepie91/blog/2015/07/19/why-you-should-never-ever-ever-use-mongodb/

Page 9: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB: DATA MODEL

Page 10: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

JavaScript Object Notation: JSON

Page 11: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Binary JSON: BSON

Page 12: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MongoDB: Datatypes

Page 13: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MongoDB: Map from keys to BSON values

TVSeries

Key BSON Value

99a88b77c66d

… …

Page 14: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

TVSeries

Key BSON Value

99a88b77c66d

11f22e33d44c

MongoDB Collection: Similar Documents

Page 15: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

database: TV

MongoDB Database: Related Collections

TVEpisodes

Key BSON Value

… …

TVNetworks

Key BSON Value

… …

TVSeries

Key BSON Value

… …

Page 16: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Load database (and create if not exists):

See all (non-empty) databases ( is empty):

See current database:

Drop current database:

Page 17: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Create collection :

See all collections in current database:

Drop collection :

Clear all documents from collection :

Page 18: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Create capped collection (keeps only most recent):

Create collection with default index on :

Page 19: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB: INSERTING DATA

Page 20: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Insert document: without

Page 21: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Insert document: with

… fails if _id already exists… use or to update existing document(s)

Page 22: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Use and to overwrite:

… overwrites old document

Page 23: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB: QUERIES WITH SELECTION

Page 24: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Query documents in a collection with :

… use to return one document

Page 25: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Pretty print results with :

Page 26: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection: find documents matching σ

σ

Page 27: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality

Equality:

Results would include but for brevity, we will omitthis from examples where it is not important.

Page 28: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Nested key

Key can access nested values:

Page 29: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality on

Equality on nested null value:

… matches when value is null or ...

Page 30: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality on

Key can access nested values:

… when field doesn't exist with .

Page 31: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality on document

Value can be an object/document:

Page 32: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality on document

Value can be an object/document:

… no results: needs to match full object.

Page 33: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality on document

Value can be an object/document:

… no results: order of attributes matters.

Page 34: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality on exact array

Equality can match an exact array:

Page 35: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality on exact array

Equality can match an exact array

… no results: needs to match full array.

Page 36: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality on exact array

Equality can match an exact array

… no results: order of elements matters.

Page 37: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality matches inside array

Equality matches a value in an array:

Page 38: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Equality matches both

Equality matches a value inside and outside an array:

*cough*

Page 39: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Inequalities

Less than:

Greater than:

Less than or equal:

Greater than or equal:

Not equals:

Page 40: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Less Than

Less than:

Page 41: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Match one of multiple values

Match any value:

… also passes if key does not exist.

Match no value:

Page 42: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Match one of multiple values

Match any value:

Page 43: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Match one of multiple values

Match any value:

… if key references an array, any value of array should match any value of

Page 44: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Boolean connectives

And: σ σ′

Or: σ σ′

Not: σ

Nor: σ σ′

… of course, can nest such conditions

Page 45: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: And

And: σ σ′

Page 46: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Attribute (not) exists

Exists:

Not Exists:

Page 47: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Attribute exists

Exists:

… checks that the field exists(even if value is )

Page 48: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Attribute not exists

Not exists:

… checks that the field doesn’t exist(empty results)

Page 49: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Arrays

All:

Match one: σ σ

Size:

Page 50: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Array contains (at least) all elements

All:

… all values are in the array

Page 51: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Array element matches (with AND)

Match one: σ σ

… one element matches all criteria (with AND)

Page 52: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Array with exact size

Size:

… only possible for exact size of array (not ranges)

Page 53: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Type of value

Type:

Page 54: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Type of value

Type:

Page 55: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Matching an array by type?

Type:

… empty… passes if any value in the array has that type

Page 56: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

https://docs.mongodb.com/manual/reference/operator/query/type/

Selection σ: Matching an array by type?

Page 57: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

https://docs.mongodb.com/manual/reference/operator/query/type/

Selection σ: Matching an array by type?

Page 58: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan
Page 59: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Other operators

Mod:

Regex:

Text search:

Where (JS):

… is executed over all documents(and should be avoided where possible)

Page 60: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Geographic features

https://docs.mongodb.com/manual/reference/operator/query-geospatial/

Page 61: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Selection σ: Bitwise features

https://docs.mongodb.com/manual/reference/operator/query-bitwise/

Page 62: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB: PROJECTION OF OUTPUT VALUES

Page 63: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Choose output values

: Output field(s) with : Suppress field(s) with

Project first matching array element I: Project first matching array element II

: Output first or last array values

https://docs.mongodb.com/manual/reference/operator/projection/

Page 64: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output certain fields

Project only certain fields:

… outputs what is available; by default also outputs field

Page 65: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output embedded fields

Project (embedded) fields:

… field is still nested in output

Page 66: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output embedded fields in array

Project (embedded) fields:

… projects from within the array.

Page 67: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Suppress certain fields

Return all but certain fields:

… cannot combine and except ...

Page 68: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Suppress certain fields

Suppress ID:

… suppresses when other fields are output

Page 69: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output first matching element

Project first matching element:

Page 70: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output first matching element

Project first matching element:

… allows to separate selection and projection criteria.

Page 71: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output first matching element

Project first matching element:

… drops array field entirely if no element is projected.

Page 72: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output first matching element

Project first matching element:

… can match on array of documents.

Page 73: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output some matching elements

Return first elements: (where > 0)

Page 74: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output some matching elements

Return last elements: (where < 0)

Page 75: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output some matching elements

Skip and return : ( , > 0)

Page 76: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Projection π: Output some matching elements

From last , return : ( < 0, > 0)

Page 77: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB: UPDATES

Page 78: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Update u: Modify fields in documents

: Set the value: Remove the key and value

: Rename the field (change the key): You don't want to know

: Increment number by : Multiply number by : Replace values less than by : Replace values greater than by

: Set to current date

https://docs.mongodb.com/manual/reference/operator/update-field/

Page 79: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Use with query criteria and update criteria:

Page 80: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Update u: Modify arrays in documents

: Adds value if not already present: Deletes first or last value

: Appends (an) item(s) to the array: Removes values from a list

: Removes values that match a condition

(Sub-)operators used for pushing/adding values:: Select first element matching query condition

: Add or push multiple values: After pushing, keep first or last values

: Sort the array after pushing: Push values to a specific array index

https://docs.mongodb.com/manual/reference/operator/update-array/

Page 81: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Update u: Modify arrays in documents

Page 82: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Update u: Modify arrays in documents

Page 83: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Update u: Modify arrays in documents

Page 84: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB: AGGREGATION AND PIPELINES

Page 85: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Aggregation: without grouping

: Array of unique values for that key

: Count the documentshttps://docs.mongodb.com/manual/reference/method/db.collection.count/

https://docs.mongodb.com/manual/reference/method/db.collection.distinct/

Page 86: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Aggregation:

Page 87: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Pipelines: Transforming Collections

https://docs.mongodb.com/manual/reference/operator/aggregation/

stage1 stage2 stageN-1

Page 88: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Pipeline ρ: Stage Operators: Filter by selection criteria σ

: Perform a projection π

: Group documents by a key/value

[used with , , , , , , etc.]

: Perform left-outer-join with another collection

: Copy each document for each array value

: Get statistics about collection

: Count the documents in the collection

: Sort documents by a given key ( | )

: Return (up to) first documents

: Return (up to) sampled documents

: Skip n documents

: Save collection to MongoDB

(more besides)

https://docs.mongodb.com/manual/reference/operator/aggregation/

Page 89: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Pipeline Aggregation: and

Page 90: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Pipeline Aggregation:

Page 91: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Pipeline Aggregation:

Page 92: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB:INDEXING

Page 93: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MongoDB Indexing

Per collection:

• Index

• Single-field Index (sorted)

• Compound Index (sorted)

• Multikey Index (for arrays)

• Geospatial Index

• Full-text Index

• Hash-based indexing (hashed)

Page 94: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MongoDB: Text Indexing Example

Page 95: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MongoDB: Text Indexing Example

Page 96: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB:DISTRIBUTION

Page 97: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MongoDB: Distribution

• "Sharding":

– Hash-based or Horizontal Ranged

(Depends on indexes)

• Replication

– Replica sets

Page 98: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan
Page 99: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

MONGODB:WHY IS IT SO POPULAR?

Page 100: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

http://db-engines.com/en/ranking

Page 101: CC5212-1 Procesamiento Masivo de Datosaidanhogan.com/teaching/cc5212-1-2018/lectures/MDP2018-09.pdfCC5212-1 PROCESAMIENTO MASIVO DE DATOS OTOÑO 2018 Lecture 9 NoSQL: MongoDB Aidan

Questions?