Aggregate Pipeline
Dr. Andrew Besmer




db.product.drop();
db.product.insert({
name: "Batman Lego Set",
qty: 1,
price: 9.99,
company: "LEGO",
category: "Toys"
});
db.product.insert({
name: "32 Piece Ratchet Set",
qty: 2,
price: 29.99,
company: "Stanley",
category: "Tools",
vendors: ["Home Depot", "Lowes"]
});
db.product.insert({
name: "Hammer",
qty: 5,
price: 5.99,
company: "Stanley",
category: "Tools",
vendors: ["Lowes", "Ace"]
});
db.product.insert({
name: "Toothpaste",
qty: 1,
price: 2.99,
company: "Crest",
category: "Health",
vendors: ["Walmart", "Walgreens"]
});
db.product.insert({
name: "Floss",
qty: 0,
price: .99,
company: "Crest",
category: "Health",
vendors: ["Publix"]
});
db.product.insert({
name: "Power Drill",
qty: 5,
price: 49.99,
category: "Tools",
vendors: ["Lowes", "Ace"]
});
db.product.insert({
name: "120 Piece Lego Set",
qty: 1,
price: 12.99,
company: "LEGO",
vendors: ["Freds", "Walmart"]
});
db.product.insert({
name: "Hot-wheels Car",
qty: 10,
price: .99
});
db.product.insert({
name: "Elite Rescue Marine Boat",
qty: 1,
price: 39.99,
category: "Toys",
company: "Matchbox",
vendors: ["Belk", "Toys R Us", "Kmart"]
});
db.collection.find();db.collection.aggregate([]);.next() - moves to the next document.pretty() - gives pretty JSON output.forEach(jsfunction) - moves cursor through results and runs jsfunction on eachdb.collection.find({});db.product.aggregate([
{
$match: {category: "Toys"}
}
]).pretty();

db.product.aggregate([
{
$match: {
price: {$lt: 1.00}
}
}
]).pretty();
db.product.aggregate([
{
$match: {
qty: {$lt: 1}
}
}
]).pretty();

_idname, company and category fields_iddb.product.aggregate([
{
$project: {
_id: 0,
name: 1,
company: 1,
category: 1
}
}
]).pretty();

_iddb.product.aggregate([
{
$project: {
_id: 0,
FullName: {
$concat: ["$company", " ", "$name"]
}
}
}
]).pretty();
db.product.aggregate([
{
$project: {
_id: 0,
name: 1,
onhand:
{
$multiply: ["$qty", "$price"]
}
}
},
{
$match: {
onhand: {$gt: 0}
}
}
]).pretty();

db.product.aggregate([
{
$project: {
name: 1,
inventory: {qty: "$qty", price: "$price"}
}
}
]).pretty();
{
"_id" : ObjectId("57e008fa905431769accaaca"),
"name" : "Batman Lego Set",
"inventory" : {
"qty" : 1,
"price" : 9.99
}
}
$max, $min, $avg, $sum$addToSet, $push$first, $last{$group: {_id: $group, prop: {$avg: "$prop"}, ... }}_id is used as the grouping property
_id: null to group all documentsdb.product.aggregate([
{
$group:
{
_id: "$category",
avgPrice: {$avg: "$price"}
}
}
]).pretty();

$sum same as $avg to sum field values$sum: 1 to count documentsdb.product.aggregate([
{
$group:
{
_id: "$company",
totQty: {$sum: "$qty"},
countProducts: {$sum: 1}
}
}
]).pretty();


$group to aggregate
db.product.aggregate([
{ $unwind: "$vendors"},
{ $group:
{
_id: "$vendors",
avgPrice: {$avg: "$price"}
}
}
]).pretty();
{$sort: {field: 1}} // ascending order{$sort: {field: -1}} // descending order
$limit - Limits the result set
$skip - Skips through document results
{$skip: 10},{$limit: 5}

// sample zipcode collection
// example document
{
"_id" : 10021,
"city" : “NEW YORK”,
"loc" : [
-73.958805,
40.768476
],
"pop" : 106564,
"state" : ”NY”
}
