db.peach.insertMany( [
{ _id: 0, productName: "Steel beam", status: "new", quantity: 10 },
{ _id: 1, productName: "Steel beam", status: "urgent", quantity: 20 },
{ _id: 2, productName: "Steel beam", status: "urgent", quantity: 30 },
{ _id: 3, productName: "Iron rod", status: "new", quantity: 15 },
{ _id: 4, productName: "Iron rod", status: "urgent", quantity: 50 },
{ _id: 5, productName: "Iron rod", status: "urgent", quantity: 10 }
] )
# 값을 넣어주고,
db.peach.aggregate( [
{ $match: { status: "urgent" } },
{ $group: { _id: "$productName", sumQuantity: { $sum: "$quantity" } } }
] )
# 이렇게 aggregate를 통해 보면, (match가 rdbms sql문의 where라고 보면 될듯)
# status가 urgent인 데이터를 먼저 추출하면 _id가 1,2,4,5이고
# 얘네의 productname이 _id가 되고(Steel beam, Iron rod) 이 두가지 종류 별로 quantity를 더하면 50, 60이 된다.
[
{ _id: 'Steel beam', sumQuantity: 50 },
{ _id: 'Iron rod', sumQuantity: 60 }
]