db.sales.insertMany( [
{ "_id":1, "date" : ISODate("2023-01-01"), "location":"대구", "customer":{"gender":"M", "age": 14},
"items":[{"name":"연필", "price":1000, "qty":2}, {"name":"노트", "price":4000, "qty":1}]},
{ "_id":2, "date" : ISODate("2023-01-01"), "location":"부산", "customer":{"gender":"W", "age": 21},
"items":[{"name":"비누", "price":5000, "qty":1}, {"name":"샴프", "price":12000, "qty":1}]},
{ "_id":3, "date" : ISODate("2023-02-01"), "location":"부산", "customer":{"gender":"M", "age": 25},
"items":[{"name":"소주", "price":2000, "qty":10}, {"name":"맥주", "price":10000, "qty":3}]},
{ "_id":4, "date" : ISODate("2023-02-01"), "location":"서울", "customer":{"gender":"W", "age": 32},
"items":[{"name":"휴대폰", "price":1000000, "qty":1}]},
{ "_id":5, "date" : ISODate("2023-02-01"), "location":"대구", "customer":{"gender":"M", "age": 55},
"items":[{"name":"냉장고", "price":2300000, "qty":1}, {"name":"TV", "price":400000, "qty":1}]},
{ "_id":6, "date" : ISODate("2023-04-01"), "location":"서울", "customer":{"gender":"W", "age": 33},
"items":[{"name":"우유", "price":5000, "qty":2}, {"name":"라면", "price":7000, "qty":2}]},
{ "_id":7, "date" : ISODate("2023-04-01"), "location":"서울", "customer":{"gender":"M", "age": 27},
"items":[{"name":"연필", "price":1000, "qty":20}, {"name":"지우개", "price":500, "qty":10}]},
{ "_id":8, "date" : ISODate("2023-04-01"), "location":"대구", "customer":{"gender":"W", "age": 22},
"items":[{"name":"생수", "price":5000, "qty":6}, {"name":"라면", "price":5000, "qty":2}]},
{ "_id":9, "date" : ISODate("2023-06-01"), "location":"부산", "customer":{"gender":"W", "age": 41},
"items":[{"name":"휴대폰", "price":1200000, "qty":1}, {"name":"컴퓨터", "price":2500000, "qty":1}]},
{ "_id":10, "date" : ISODate("2023-06-01"), "location":"서울", "customer":{"gender":"M", "age": 24},
"items":[{"name":"컴퓨터", "price":1000000, "qty":1}, {"name":"소주", "price":2000, "qty":5}]}
]);
db.sales.find({location:"부산"} , {date:1 , "items.name":1})
db.sales.aggregate([ { $unwind: "$items" }, { $project: { items: { name: 1, amount: { $multiply: ["$items.price", "$items.qty"] } } } }, { $sort: { "items.amount": -1 } }])
4.지역별로 매출액의 합을 구한후 지역이름의 오름차순으로 출력하라 -> db.sales.aggregate([ {$unwind:"$items"}, {$group :{_id:"$location" ,"매출액" :{$sum : {$multiply:["$items.price","$items.qty"]}}}}, {$sort:{location:1}}])