05 - MongoDB Aggregation Flashcards

1
Q

Syntax when using pipeline

A
collection = "tweets"
pipeline = [$stage1, $stage2, ...$stageN]
db.<COLLECTION>.aggregate( pipeline, { options } )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

$match

A

Similar to find()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

$project

A

Shape documents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

$sort

A

Like the cursor method of the same name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define & list

$group

LIst - 4

A

Used to aggregate field values from multiple documents
$sum
$avg
$min
$max

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

$limit

A

Used to limit the amount of documents returned

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

$unwind

A

Separates documents with arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

$out

A

Specifies an output collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Similar to find()

A

$match

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Shape documents

A

$project

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Like the cursor method of the same name

A

$sort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Used to aggregate field values from multiple documents
$sum
$avg
$min
$max

A

Define & list

$group

LIst - 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Used to limit the amount of documents returned

A

$limit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Separates documents with arrays

A

$unwind

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Specifies an output collection

A

$out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Code

Count number of action movies (with $count)

A
db.moviesSmall.aggregate([{"$match" : {"category" : "action"}}, {"$count" : "movies"}])
17
Q

Find the rating of the highest rated expensive movie
(aggregate)
budget
imdb_rating

A

`db.moviesSmall.aggregate([{

“$match” : {“budget” : {“$gt” : 25}}},

{“$group” : {“_id” : “movies”,

“highest” : {“$max” : “$imdb_rating”}}}])`

18
Q

Get class with the most number of students

A
db.grades.aggregate([{"$group" : {"_id" : "$class_id", "num_students" : {"$sum" : 1}}}, {"$group" : {"_id" : 1, "most" :  { "$max" : "$num_students"}}}])
19
Q

Get class with the most number of students (sort)
aggregate
class_id
num_students

A

db.grades.aggregate([{"$group" : {"_id" : "$class_id", "num_students" : {"$sum" : 1}}}, { "$sort" : {"num_students" : -1}}, { "$limit" : 1}])