Sales collection dataset
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }
db.sales.aggregate(
[
{
$group : {
_id : null,
totalcost : {$sum : { $multiply: [ "$price", "$quantity" ] } }
}
}
]
)
{ "_id" : null, "totalcost" : 270 }
db.sales.aggregate(
[
{
$group : {
_id : { price: "$price" },
totalqty : {$sum : "$quantity" },
count: { $sum: 1 }
}
}
]
)
{ "_id" : { "price" : 10 }, "totalqty" : 10, "count" : 1 }
{ "_id" : { "price" : 5 }, "totalqty" : 30, "count" : 2 }
{ "_id" : { "price" : 20 }, "totalqty" : 1, "count" : 1 }
db.sales.aggregate(
[
{
$group : {
_id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
}
]
)
{ "_id" : { "month" : 4, "day" : 4, "year" : 2014 }, "totalPrice" : 200, "averageQuantity" : 15, "count" : 2 }
{ "_id" : { "month" : 3, "day" : 15, "year" : 2014 }, "totalPrice" : 50, "averageQuantity" : 10, "count" : 1 }
{ "_id" : { "month" : 3, "day" : 1, "year" : 2014 }, "totalPrice" : 20, "averageQuantity" : 1, "count" : 1 }
db.sales.aggregate(
[
{
$group : {
_id : { price: "$price" },
totalqty : {$sum : "$quantity" },
count: { $sum: 1 }
}
},
{"$sort" : {"totalqty" : 1 }}
]
)
{ "_id" : { "price" : 20 }, "totalqty" : 1, "count" : 1 }
{ "_id" : { "price" : 10 }, "totalqty" : 10, "count" : 1 }
{ "_id" : { "price" : 5 }, "totalqty" : 30, "count" : 2 }
db.sales.aggregate(
[
{
$group : {
_id : { price: "$price" },
totalqty : {$sum : "$quantity" },
count: { $sum: 1 }
}
},
{"$sort" : {"totalqty" : 1 }},
{"$match" : {"totalqty" : {$gte : 10} }}
]
)
{ "_id" : { "price" : 10 }, "totalqty" : 10, "count" : 1 }
{ "_id" : { "price" : 5 }, "totalqty" : 30, "count" : 2 }
db.sales.aggregate(
[
{
$group : {
_id : { price: "$price" },
totalqty : {$sum : "$quantity" },
count: { $sum: 1 }
}
},
{"$sort" : {"totalqty" : 1 }},
{"$match" : {"totalqty" : {$gte : 10} }},
{$sample : {size : 3}}
]
)
{ "_id" : { "price" : 5 }, "totalqty" : 30, "count" : 2 }
{ "_id" : { "price" : 10 }, "totalqty" : 10, "count" : 1 }
db.sales.aggregate(
[
{
$group : {
_id : { price: "$price" },
totalqty : {$sum : "$quantity" },
count: { $sum: 1 }
}
},
{"$sort" : {"totalqty" : 1 }},
{"$match" : {"totalqty" : {$gte : 10} }},
{$sample : {size : 3}}
]
)