Академический Документы
Профессиональный Документы
Культура Документы
Alvin Richards
complex documents
Aggregation - Pipelines
Aggregation requests specify a pipeline A pipeline is a series of operations Members of a collection are passed
Example - twitter
{
"_id"
:
ObjectId("4f47b268fb1c80e141e9888c"),
"user"
:
{
"friends_count"
:
73,
"location"
:
"Brazil",
"screen_name"
:
"Bia_cunha1",
"name"
:
"Beatriz
Helena
Cunha",
"followers_count"
:
102,
} }
Example - twitter
db.tweets.aggregate(
{$match:
{"user.friends_count":
{
$gt:
0
},
"user.followers_count":
{
$gt:
0
}
}
},
{$project:
{
location:
"$user.location",
friends:
"$user.friends_count",
followers:
"$user.followers_count"
}
},
{$group:
{_id:
"$location",
friends:
{$sum:
"$friends"},
followers:
{$sum:
"$followers"}
}
} );
Example - twitter
db.tweets.aggregate(
{$match:
{"user.friends_count":
{
$gt:
0
},
"user.followers_count":
{
$gt:
0
}
}
},
{$project:
{
location:
"$user.location",
friends:
"$user.friends_count",
followers:
"$user.followers_count"
}
},
{$group:
{_id:
"$location",
friends:
{$sum:
"$friends"},
followers:
{$sum:
"$followers"}
}
} );
Predicate
Example - twitter
db.tweets.aggregate(
{$match:
{"user.friends_count":
{
$gt:
0
},
"user.followers_count":
{
$gt:
0
}
}
},
{$project:
{
location:
"$user.location",
friends:
"$user.friends_count",
followers:
"$user.followers_count"
}
},
{$group:
{_id:
"$location",
friends:
{$sum:
"$friends"},
followers:
{$sum:
"$followers"}
}
} );
Predicate
Example - twitter
db.tweets.aggregate(
{$match:
{"user.friends_count":
{
$gt:
0
},
"user.followers_count":
{
$gt:
0
}
}
},
{$project:
{
location:
"$user.location",
friends:
"$user.friends_count",
followers:
"$user.followers_count"
}
},
{$group:
{_id:
"$location",
friends:
{$sum:
"$friends"},
followers:
{$sum:
"$followers"}
}
} );
Predicate
Parts of the document you want to project Function to apply to the result set
Example - twitter
{
...
} "result"
:
[
{
"_id"
:
"Far
Far
Away",
"friends"
:
344,
"followers"
:
789
}, ], "ok"
:
1
10
Pipeline Operations
$match
Uses a query predicate (like .nd({})) as a lter
$project
Uses a sample document to determine the shape
$unwind
Hands out array elements one at a time
$group
Aggregates items into buckets dened by a key
11
$limit
Only allow the specied number of
documents to pass
$skip
Skip over the specied number of documents
12
Computed Expressions
Available in $project operations Prex expression language
$add:[$eld1, $eld2] $ifNull:[$eld1, $eld2] Nesting:
13
Computed Expressions
String functions
$toUpper, $toLower, $substr
predicate
14
Projections
$project can reshape results
Include or exclude elds Computed elds Arithmetic expressions Pull elds from nested documents to the top Push elds from the top down into new virtual documents
15
Unwinding
$unwind can stream arrays
Array values are doled out one at time in the
context of their surrounding documents Makes it possible to lter out elements before returning
16
Grouping
$group aggregation expressions
Dene a grouping key as the _id of the result Total grouped column values: $sum Average grouped column values: $avg Collect grouped column values in an array or
17
Sorting
$sort can sort documents
Sort specications are the same as today,
18
Demo
Demo
les
are
at
https://gist.github.com/2036709
19
Usage Tips
Use $match in a pipeline as early as
possible
possible
20
Driver Support
Initial version is a command
For any language, build a JSON database
21
22
Sharding support
Initial release supports sharding Mongos analyzes pipeline
forwards operations up to $group or $sort to
23
24
Aggregation+Framework
https://jira.mongodb.org/browse/SERVER/
component/10840
25
download at mongodb.org
alvin@10gen.com
http://bit.ly/mongoE
http://linkd.in/joinmongo
26