-
Type:
Task
-
Resolution: Fixed
-
Priority:
Minor - P4
-
Affects Version/s: None
-
Component/s: Documentation
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Here are the examples. They should be delimited with
// Start <delimiter> [code] // End <delimiter>
as has been done for the CRUD examples and such. I've attached the mongoexported datasets I used for the agg examples if one wants to make sure everything is working.
| Delimiter | Shell Snippet | Notes |
|---|---|---|
| Aggregation Example 1 | db.sales.aggregate(
[
{ $match : { "items.fruit":"banana" } },
{ $sort : { "date" : 1 } }
])
|
Simple agg example |
| Aggregation Example 2 | db.sales.aggregate(
[
{
$unwind: "$items"
},
{ $match: {
"items.fruit" : "banana",
}},
{
$group : {
_id : { day: { $dayOfWeek: "$date" } },
count: { $sum: "$items.quantity" }
}
},
{
$project: {
dayOfWeek: "$_id.day",
numberSold: "$count",
_id:0
}
},
{
$sort: { "numberSold": 1 }
}
]
)
|
Aggregation - $match, $group, $project, $unwind, $sum, $sort, $dayOfWeek |
| Aggregation Example 3 | db.sales.aggregate([
{
$unwind: "$items"
},
{
$group : {
_id : { day : { $dayOfWeek : "$date" } },
items_sold : { $sum : "$items.quantity" },
revenue: { $sum : { $multiply : [ "$items.quantity", "$items.price" ] } }
}
},
{
$project : {
day : "$_id.day",
revenue : 1,
items_sold : 1,
discount: {
$cond : { if : { $lte : [ "$revenue", 250 ] }, then : 25, else : 0 }
}
}
}
])
|
Aggregation - $unwind, $group, $sum, $dayOfWeek, $multiply, $project, $cond. If you're testing this one, change $revenue, 250 to a smaller number since the dataset I've attached doesn't have that many documents currently |
| Aggregation Example 4 | db.air_alliances.aggregate( [
{
$lookup : {
from : "air_airlines",
let : { constituents: "$airlines" },
pipeline : [
{
$match : { $expr : { $in : [ "$name", "$$constituents" ] } }
}
],
as : "airlines"
}
},
{
$project : {
"_id" : 0,
"name" : 1,
airlines : {
$filter : {
input : "$airlines",
as : "airline",
cond : { $eq: ["$$airline.country", "Canada"] }
}
}
}
}
])
|
Aggregation - $lookup, $filter, $match |
| runCommand Example 1 | db.runCommand({buildInfo: 1})
|
|
| runCommand Example 2 | db.runCommand({collStats:"restaurants"})
|
|
| Index Example 1 |
db.records.createIndex( { score: 1 } )
|
Indexes - builds simple ascending index |
| Index Example 2 |
db.restaurants.createIndex(
{ cuisine: 1, name: 1 },
{ partialFilterExpression: { rating: { $gt: 5 } } }
)
|
Indexes - builds multikey index with partial filter expression |
- is depended on by
-
DRIVERS-448 Aggregation, runCommand, and index management examples for Docs
-
- Closed
-