CSC 369: Distributed Computing Alex Dekhtyar April 20 Day 7: - - PowerPoint PPT Presentation

csc 369 distributed computing
SMART_READER_LITE
LIVE PREVIEW

CSC 369: Distributed Computing Alex Dekhtyar April 20 Day 7: - - PowerPoint PPT Presentation

CSC 369: Distributed Computing Alex Dekhtyar April 20 Day 7: MongoDB Aggregation Pipeline, Part II Housekeeping: plan for the week April 20 Grading: Lecture: joins Lab 1: not certain Lab: practice Lab 2: end of the week


slide-1
SLIDE 1

CSC 369: Distributed Computing

Alex Dekhtyar

Day 7: MongoDB Aggregation Pipeline, Part II

April 20

slide-2
SLIDE 2
slide-3
SLIDE 3

Housekeeping: plan for the week

April 20

  • Lecture: joins
  • Lab: practice
  • Assignment: Lab 3 (Lab 3-2 by Tuesday)

April 22

  • Lecture: catch-all on MongoDB, Mongo Python connectivity
  • Lab: practice
  • Assignment: Lab 3

April 24

  • Lecture: MongoDB quiz (?)
  • Lab: practice
  • Assignment: Lab 3 and Lab 4 (build a MongoDB-supported application)

Grading:

Lab 1: not certain Lab 2: end of the week

April 27 (Monday)

  • MongoDB test
  • Flip lecture/lab
slide-4
SLIDE 4

Recap of Last Week

slide-5
SLIDE 5

Data Manipulation operations

Filtering Projection Transformation Join Grouping Sort

Given a condition - keep only objects that satisfy it Modify the contents of its object based solely on what’s in the object itself

Aggregation

Break collection into groups, each representing

  • bjects with same values of some keys

Combine objects from two different collections based on matches in values of some keys Compute an aggregate value over a set of objects Return objects in a specific order

slide-6
SLIDE 6

Data Manipulation Operations

Ungrouping Unwinding Limit Skip

Opposite of grouping - build an object for each element of an array Return a specific number of documents

Sample

Return documents after skipping a specified number Return a random sample of documents

slide-7
SLIDE 7

$operation

Filtering Projection Join Grouping Sort Aggregation $match $project $group $lookup $sort Unwinding Limit Skip Sample $unwind $limit $skip $sample

slide-8
SLIDE 8

$operation

Filtering Projection Join Grouping Sort Aggregation $match $project $set $unset $addFields $redact $replaceRoot $group $bucket $bucketAuto $lookup $graphLookup $sortByCount $sort

slide-9
SLIDE 9

$operation

Filtering Projection Join Grouping Sort Aggregation $match $project $set $unset $addFields $redact $replaceRoot $group $bucket $bucketAuto $lookup $graphLookup $sortByCount $sort

slide-10
SLIDE 10

$operation

Join $lookup Faceted Filter $facet Output Control $out $merge

slide-11
SLIDE 11

Joins

Combine information from multiple data sources In Relational Algebra: cartesian product followed by selection

slide-12
SLIDE 12

Joins (Relational Algebra Example)

Order Product Price

1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating

1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

slide-13
SLIDE 13

Joins (Relational Algebra Example)

Order Product Price

1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating

1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products What product was purchased? “1001” is NOT a good answer

slide-14
SLIDE 14

Joins (Relational Algebra Example)

Order Product Price

1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating

1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

slide-15
SLIDE 15

Joins (Relational Algebra Example)

Order Product Price

1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating

1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

slide-16
SLIDE 16

Joins (Relational Algebra Example)

Order Product Price

1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating

1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order Product Price Name Rating 1234 1001 $4.99 Hand Sainitizer 4.5 1234 2031 $20.00 Masks 3 3403 2031 $20.00 Masks 3 ... ... ... ... ...

slide-17
SLIDE 17

Joins (Relational Algebra Example)

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order O.Product Price P.Product Name Rating

1234 1001 $4.99 1001 Hand Sanitizer 4.5 1234 1001 $4.99 1100 Gloves 4 1234 1001 $4.99 2031 Masks 3 1234 1001 $4.99 4011 Teddy Bear 5 1234 2031 $20.00 1001 Hand Sanitizer 4.5

slide-18
SLIDE 18

Joins (Relational Algebra Example)

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order O.Product Price P.Product Name Rating

1234 1001 $4.99 1001 Hand Sanitizer 4.5 1234 1001 $4.99 1100 Gloves 4 1234 1001 $4.99 2031 Masks 3 1234 1001 $4.99 4011 Teddy Bear 5 1234 2031 $20.00 1001 Hand Sanitizer 4.5

slide-19
SLIDE 19

Joins (Relational Algebra Example)

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order O.Product Price P.Product Name Rating

1234 1001 $4.99 1001 Hand Sanitizer 4.5 1234 1001 $4.99 1100 Gloves 4 1234 1001 $4.99 2031 Masks 3 1234 1001 $4.99 4011 Teddy Bear 5 1234 2031 $20.00 1001 Hand Sanitizer 4.5

slide-20
SLIDE 20

Joins (Relational Algebra Example)

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order O.Product Price P.Product Name Rating

1234 1001 $4.99 1001 Hand Sanitizer 4.5 1234 1001 $4.99 1100 Gloves 4 1234 1001 $4.99 2031 Masks 3 1234 1001 $4.99 4011 Teddy Bear 5 1234 2031 $20.00 1001 Hand Sanitizer 4.5

Cartesian product Selection

slide-21
SLIDE 21

Joins (Relational Algebra Example)

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order O.Product Price P.Product Name Rating

1234 1001 $4.99 1001 Hand Sanitizer 4.5 1234 1001 $4.99 1100 Gloves 4 1234 1001 $4.99 2031 Masks 3 1234 1001 $4.99 4011 Teddy Bear 5 1234 2031 $20.00 1001 Hand Sanitizer 4.5

Cartesian product Selection

slide-22
SLIDE 22

Joins (Relational Algebra Example)

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order O.Product Price Name Rating

1234 1001 $4.99 Hand Sanitizer 4.5 1234 1001 $4.99 Gloves 4 ... ... ... ... ... 1234 1001 $4.99 Teddy Bear 5 1234 2031 $20.00 Hand Sanitizer 4.5

Cartesian product Selection Projection

slide-23
SLIDE 23

Inner Joins vs Outer Joins

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order Product Price Name Rating 1234 1001 $4.99 Hand Sanitizer 4.5 1234 2031 $20.00 Masks 3 3403 2031 $20.00 Masks 3 ... ... ... ... ...

For each product ordered report the name of the product and the rating.

slide-24
SLIDE 24

Inner Joins vs Outer Joins

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order Price Product Name Rating 1234 $4.99 1001 Hand Sanitizer 4.5 3311 $12.00 1100 Gloves 4 1234 $20.00 2031 Masks 3 3403 $20.00 2031 Masks 3

For each product, report the details of all orders.

slide-25
SLIDE 25

Inner Joins vs Outer Joins

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order Price Product Name Rating 1234 $4.99 1001 Hand Sanitizer 4.5 3311 $12.00 1100 Gloves 4 1234 $20.00 2031 Masks 3 3403 $20.00 2031 Masks 3

For each product, report the details of all orders.

slide-26
SLIDE 26

Inner Joins vs Outer Joins

Order Product Price 1234 1001 $4.99 1234 2031 $20.00 3403 2031 $20.00 3311 1100 $12.00

Orders

Product Name Rating 1001 Hand Sanitizer 4.5 1100 Gloves 4 2031 Masks 3 4011 Teddy Bear 5

Products

Order Price Product Name Rating 1234 $4.99 1001 Hand Sanitizer 4.5 3311 $12.00 1100 Gloves 4 1234 $20.00 2031 Masks 3 3403 $20.00 2031 Masks 3 NULL NULL 4011 Teddy Bear 5

For each product, report the details of all orders. Keep all products in output Right outer join

slide-27
SLIDE 27

Joins in MongoDB

{$lookup: { from: <collection>, localField: <field>, foreignField: <field>, as: <arrayField> } }

slide-28
SLIDE 28

Joins in MongoDB

{$lookup: { from: <collection>, localField: <field>, foreignField: <field>, as: <arrayField> } }

Left outer join

slide-29
SLIDE 29

Joins in MongoDB

{$lookup: { from: <collection>, localField: <field>, foreignField: <field>, as: <arrayField> } }

Left outer join

Join condition: localField == collection.foreignField

slide-30
SLIDE 30

Joins in MongoDB

{$lookup: { from: <collection>, localField: <field>, foreignField: <field>, as: <arrayField> } }

Left outer join

Join condition: localField == collection.foreignField Matched documents are imported into <arrayField>

slide-31
SLIDE 31

Joins in MongoDB

{$lookup: { from: <collection>, localField: <field>, foreignField: <field>, as: <arrayField> } }

Left outer join

Join condition: localField == collection.foreignField Matched documents are imported into <arrayField>

EXAMPLE

slide-32
SLIDE 32

db.spring.aggregate( {$project: {_id:0, name:1, department:1}}, {$lookup: { from: "departments", localField:"department", foreignField: "department", as: "deptInfo" } }, {$unwind: "$deptInfo"} )

slide-33
SLIDE 33

db.departments.aggregate( {$lookup: {from: "spring", localField: "department", foreignField: "department", as: "faculty" } } )

slide-34
SLIDE 34

Facets: set up multiple independent pipelines

{$facet: { key1: [<stage1>, <stage2>,...,<stageK1>], key2: [<stage1>, <stage2>,...,<stageK2>], ... keyN: [<stage1>, <stage2>,...,<stageKn>] } }