Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save liyuqi/6bc29492fb5a569e9cfe to your computer and use it in GitHub Desktop.
Save liyuqi/6bc29492fb5a569e9cfe to your computer and use it in GitHub Desktop.
#5 Aggregations in 5 Minutes: The Nickel Tour of the Aggregation Framework
###Sample Document
```
mongos> db.logs.findOne()
{
"_id" : ObjectId("556172a53004b760dde8a488"),
"d_id" : ObjectId("556172a53004b760dde8a443"),
"v" : 6205,
"timestamp" : ISODate("3129-12-13T02:03:17.906Z"),
"location" : {
"type" : "Point",
"coordinates" : [
-174.95596353219008,
40.654427078258834
]
}
}
```
##1
###A simple projection
```
var project = { $project: { device: "$d_id", value: "$v", _id : 0 } }
db.logs.aggregate( project )
```
###Results
```
mongos> db.logs.aggregate( project )
{ "device" : ObjectId("556172a53004b760dde8a443"), "value" : 6205 }
{ "device" : ObjectId("556172a53004b760dde8a43f"), "value" : 3467 }
{ "device" : ObjectId("556172a53004b760dde8a457"), "value" : 3535 }
{ "device" : ObjectId("556172a53004b760dde8a45d"), "value" : 5196 }
{ "device" : ObjectId("556172a53004b760dde8a430"), "value" : 5063 }
{ "device" : ObjectId("556172a53004b760dde8a45b"), "value" : 3429 }
{ "device" : ObjectId("556172a53004b760dde8a433"), "value" : 2791 }
{ "device" : ObjectId("556172a53004b760dde8a448"), "value" : 3024 }
{ "device" : ObjectId("556172a53004b760dde8a43b"), "value" : 6727 }
{ "device" : ObjectId("556172a53004b760dde8a46d"), "value" : 7381 }
{ "device" : ObjectId("556172a53004b760dde8a43a"), "value" : 3193 }
{ "device" : ObjectId("556172a53004b760dde8a463"), "value" : 7480 }
{ "device" : ObjectId("556172a53004b760dde8a485"), "value" : 1925 }
{ "device" : ObjectId("556172a53004b760dde8a487"), "value" : 2748 }
{ "device" : ObjectId("556172a53004b760dde8a42b"), "value" : 4152 }
{ "device" : ObjectId("556172a53004b760dde8a453"), "value" : 4699 }
{ "device" : ObjectId("556172a53004b760dde8a447"), "value" : 7381 }
{ "device" : ObjectId("556172a53004b760dde8a427"), "value" : 1190 }
{ "device" : ObjectId("556172a53004b760dde8a43c"), "value" : 7392 }
{ "device" : ObjectId("556172a53004b760dde8a475"), "value" : 8124 }
Type "it" for more
```
##2
###Grouping
```
var group = { $group: {_id: "$device", average: { "$avg": "$value"} } }
```
###Results
```
mongos> db.logs.aggregate( project, group )
{ "_id" : ObjectId("556172a53004b760dde8a42c"), "average" : 5545.707070707071 }
{ "_id" : ObjectId("556172a53004b760dde8a474"), "average" : 4978.903225806452 }
{ "_id" : ObjectId("556172a53004b760dde8a441"), "average" : 4680.555555555556 }
{ "_id" : ObjectId("556172a53004b760dde8a464"), "average" : 4449.392523364486 }
{ "_id" : ObjectId("556172a53004b760dde8a461"), "average" : 4996.213483146067 }
{ "_id" : ObjectId("556172a53004b760dde8a456"), "average" : 5068.40206185567 }
{ "_id" : ObjectId("556172a53004b760dde8a446"), "average" : 4885.193548387097 }
{ "_id" : ObjectId("556172a53004b760dde8a424"), "average" : 5428.291262135922 }
{ "_id" : ObjectId("556172a53004b760dde8a439"), "average" : 4490.298850574713 }
{ "_id" : ObjectId("556172a53004b760dde8a429"), "average" : 5121.046728971963 }
{ "_id" : ObjectId("556172a53004b760dde8a44b"), "average" : 4806.734042553191 }
{ "_id" : ObjectId("556172a53004b760dde8a462"), "average" : 4761.1612903225805 }
{ "_id" : ObjectId("556172a53004b760dde8a469"), "average" : 4435.75 }
{ "_id" : ObjectId("556172a53004b760dde8a47b"), "average" : 5881.725490196079 }
{ "_id" : ObjectId("556172a53004b760dde8a470"), "average" : 5089.471910112359 }
{ "_id" : ObjectId("556172a53004b760dde8a47f"), "average" : 5509.420168067227 }
{ "_id" : ObjectId("556172a53004b760dde8a458"), "average" : 4940.489795918367 }
{ "_id" : ObjectId("556172a53004b760dde8a455"), "average" : 4331.29347826087 }
{ "_id" : ObjectId("556172a53004b760dde8a476"), "average" : 4835.9795918367345 }
{ "_id" : ObjectId("556172a53004b760dde8a465"), "average" : 5084.881818181818 }
Type "it" for more
```
##3
### Add To Set
```
var projectTypes = {
$project: {
device: "$d_id",
value: "$v",
_id : 0,
type: "$type"
}
}
var add2Set = {
$group: {
_id: "",
sensorType: {
$addToSet: "$type"
}
}
}
```
###Result
```
> db.devices.aggregate( projectTypes, add2Set );
{ "_id" : "",
"sensorType" : [
"temp",
"humidity",
"sound",
"light",
"pressure"
]
}
```
##4 Match by Geo Location
Return all sensors within 5 kilometers of my position
```
mongos> var loc = { type: "Point", coordinates: [ -174.9559, 40.6544 ] }
mongos> var geoMatch = {
location: {
"$geoNear": { "$geometry": point, maxDistance: 500000 }
}
}
mongos> var match = { $match: geoMatch }
mongos> db.logs.aggregate( match )
```
###Result
```
too much to show here
```
##5 Output To A Collection
```
mongos> var iCommandTheeOutDemon = { $out: "paths" };
mongos> db.logs.aggregate( groupByDevice, projectGeoJson, iCommandTheeOutDemon )
```
### Results
Find one sample document.
```
mongos> db.paths.findOne()
{
"_id" : ObjectId("556172a53004b760dde8a42c"),
"coordinates" : [
[
-14.460004595311432,
53.96892154407959
],
[
-127.88437751912434,
-13.702923069362384
],
...I
```
## Bonus Aggregation: Calculate Variance!
```
{ "$project" : {
mean: "$meanSpd",
spdDiffSqrd : {
"$map" : {
"input": {
"$map" : {
"input" : "$speeds",
"as" : "samp",
"in" : { "$subtract" : [ "$$samp", "$meanSpd" ] }
}
},
as: "df", in: { $multiply: [ "$$df", "$$df" ] }
} } } },
{ $unwind: "$spdDiffSqrd" },
{ $group: { _id: mean: "$mean", variance: { $avg: "$spdDiffSqrd" } } }
```
##Fun Fact:
Each pipeline operator is an Abstract Syntax Tree (AST) represented in JSON!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment