MongoDB DML 예제

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}]}
]);

  1. 대구에서 발생한 매출 정보 -> db.sales.find({location : "대구"})
  2. 연필과 노트를 모두 포함한 매출 정보 -> db.sales.find({ "items.name": { $all: ["연필", "노트"] } })
  3. price가 백만원을 초과한 휴대폰의 매출정보 -> db.sales.find({ "items.name": "휴대폰", "items.price": { $gt: 1000000 } })
  4. 2020 4월에 발생한 매출정보를 출력하라 -> db.sales.find({ "date": { $gte: ISODate("2023-04-01"), $lt: ISODate("2023-05-01") } })
  5. 소주를 구매한 customer정보를 출력하라 -> db.sales.find({"items.name" : "소주"} , {customer : 1})
  6. 10대남성과 50대 남성이 구매한 Items -> db.sales.find({ $or: [ {"customer.age": 10, "customer.gender": "M"}, {"customer.age": 50, "customer.gender": "M"} ] }, { "_id": 0, "items": 1 })
  7. price가 5000인 라면의 매출정보 -> db.sales.find({ "items": { $elemMatch: { "name": "라면", "price": 5000 } } })
  8. 아이템 배열의 크기가 1인 매출 정보 -> db.sales.find({ "items": { $size: 1 } })
  9. 부산에서 발생한 매출 기록에 대해 날짜와 상품 이름만 출력

Aggregation

  1. price가 1000000원을 초과한 휴대폰의 매출정보 -> db.sales.aggregate([ { $match: { "items.name": "휴대폰", "items.price": { $gt: 1000000 } } } ])
  2. 여성이 구매한 상품의 매출 건수를 출력하라 -> db.sales.aggregate([ {$match:{"customer.gender":'W'}}, {$count:"여성이 구매한 판매 건수"}])
  3. $unwind연산은 배열의 내용을 펼치는 것이다 $project 연산과 연동하여 상품이름과 매출액을 구한후 매출액의 내림차순으로 정렬하여 출력하라

4.지역별로 매출액의 합을 구한후 지역이름의 오름차순으로 출력하라 -> db.sales.aggregate([ {$unwind:"$items"}, {$group :{_id:"$location" ,"매출액" :{$sum : {$multiply:["$items.price","$items.qty"]}}}}, {$sort:{location:1}}])

SQL 문법 비교

  1. SELECT COUNT(*) AS count FROM orders --> db.orders.aggregate([{$group : {__id : null , count : $count:{}}}])