MongoDB Documentation, Release 3.0.4 SQL Example MongoDB Example Description SELECT COUNT ( * ) AS count FROM orders db.orders.aggregate( [ { $group: { _id: null , count: { $sum: 1 } } } ] ) Count all records from orders SELECT SUM (price) AS total FROM orders db.orders.aggregate( [ { $group: { _id: null , total: { $sum: "$price" } } } ] ) Sum the price field from orders SELECT cust_id, SUM (price) AS total FROM orders GROUP BY cust_id db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } } ] ) For each unique cust_id , sum the price field. SELECT cust_id, SUM (price) AS total FROM orders GROUP BY cust_id ORDER BY total db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } }, { $sort: { total: 1 } } ] ) For each unique cust_id , sum the price field, results sorted by sum. SELECT cust_id, ord_date, SUM (price) AS total FROM orders GROUP BY cust_id, ord_date db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id" , ord_date: { month: { $month : "$ord_date" }, day: { $dayOfMonth : "$ord_date" }, year: { $year : "$ord_date"} } }, total: { $sum: "$price" } } } ] ) For each unique cust_id , ord_date grouping, sum the price field. Excludes the time portion of the date. SELECT cust_id, count ( * ) FROM orders db.orders.aggregate( [ { $group: { For cust_id with multiple records, return the cust_id and the corre- sponding record count. 7.4. Aggregation Reference 477