---
# System prepended metadata

title: 'MongoDB Aggregation Framework #M121'
tags: [mongodb]

---

# MongoDB Aggregation Framework #M121


> https://ithelp.ithome.com.tw/articles/10185952


To connect to M121 course Atlas Cluster,
using the mongodb shell:

```
mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl -u m121 -p aggregations --norc
```

```
$ show dbs
100YWeatherSmall  0.128GB
admin             0.000GB
aggregations      0.067GB
citibike          0.367GB
city              0.002GB
config            0.000GB
coursera-agg      0.083GB
local             0.940GB
mflix             0.449GB
results           0.000GB
ships             0.001GB
video             0.513GB
```
```
$ show collections
air_airlines
air_alliances
air_routes
bronze_banking
child_reference
customers
employees
exoplanets
gold_banking
icecream_data
movies
nycFacilities
parent_reference
silver_banking
solarSystem
stocks
system.profile
system.views
```


### [Aggregation Pipeline Quick Reference](https://docs.mongodb.com/manual/meta/aggregation-quick-reference/)

> Documents flow through the pipeline, passing one stage to the next

> The Aggregation Framework provides us many stages to filter and transform our data.

#### The Concept of Pipelines

- Composition of stages
- Configurable for transformation
- Flow like an assembly line
- Arranged in multiple ways

![](https://i.imgur.com/RXyt6w8.png)

- An aggregation pipeline is an array of stages.

- Some expressions can only be used in certian stages.
    > This is correct. For example, accumulator expressions can only be used within the $group stage, with select accumulator expressions available in the $project stage. You'll learn about these stages in depth in the course!

#### $match: Filtering documents
```js=
// $match all celestial bodies, not equal to Star

db.solarSystem.aggregate([{
  "$match": { "type": { "$ne": "Star" } }
}]).pretty()
```
```js=
// using $count
db.solarSystem.aggregate([{
  "$match": { "type": { "$ne": "Star"} }
}, {
  "$count": "planets"
}]);
```
![](https://i.imgur.com/6Jn6czC.png)




###### tags: `mongodb`