-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Component/s: Docs Examples
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}) |
|
db.runCommand({collStats:"restaurants"})
|
No longer applicable. See DRIVERS-2232. | |
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 |
- depends on
-
CDRIVER-2500 Aggregation, runCommand, and index management examples for Docs
- Closed
-
CSHARP-2183 Aggregation, runCommand, and index management examples for Docs
- Closed
-
GODRIVER-225 Aggregation, runCommand, and index management examples for Docs
- Closed
-
JAVA-2781 Aggregation, runCommand, and index management examples for Docs
- Closed
-
MOTOR-195 Aggregation, runCommand, and index management examples for Docs
- Closed
-
NODE-1323 Aggregation, runCommand, and index management examples for Docs
- Closed
-
PHPLIB-326 Aggregation, runCommand, and index management examples for Docs
- Closed
-
RUBY-1296 Aggregation, runCommand, and index management examples for Docs
- Closed
-
RUST-537 Aggregation, runCommand, and index management examples for Docs
- Closed
-
CXX-1514 Aggregation, runCommand, and index management examples for Docs
- Closed
-
PYTHON-1465 Aggregation, runCommand, and index management examples for Docs
- Closed