Notice
Recent Posts
Recent Comments
영혼의 요양소
140530 MongoDB 연습 문제 본문
2014년 5월 30일
Mongo DB 연습 문제/몽고 디비/NoSQL
//먼저 cmd에서 mongo를 입력해 켜고, //아래 DB를 한 줄씩 입력한다. db.Score.insert({name:"aaa",kor:90,eng:81,mat:98}) db.Score.insert({name:"bbb",kor:100,eng:100,mat:76}) db.Score.insert({name:"ccc",kor:80,eng:55,mat:67}) db.Score.insert({name:"ddd",kor:70,eng:69,mat:89}) db.Score.insert({name:"eee",kor:60,eng:80,mat:78}) // 집합연산을 구현한다. - count(), distinct(), sort() + limit(), - group({key, reduce, initial, [keyf,][cond,]finalize}) a. key: 필드 이름 ex) {score:true} bl. reduce : 그룹으로 묶은 문서에 집합연산 ex)reduce : function(cur, result) cur: 현재 문서, result : 결과 저장 문서 c. initial : reduce의 result의 초기값을 지정 d. keyf : key 필드를 대처하는 자바 스크립트 객체 e. cond : 특정 조건에 맞는 문서의 그룹 ex) {grad : {$gte:4}} : 4학년 이상의 학생에 대한 그룹 f. finalize : 결과의 최종 결과값 계산.(reduce의 결과를 가진다) //////////////////////////////////////////////////////////// 1. Score의 전체 출력을 해보자 db.Score.find(); - 목록 출력 db.Score.find().count; - 숫자. 2. Score의 이름과 수학 점수만 출력해보자. db.Score.find({},{name:1,mat:1}); 3. Score의 수학점수 중 70점 이상만 출력해보자. db.Score.find({mat :{$gte :70}}).count() db.Score.find({mat :{$gte :70}}) 4. Score의 이름과 국어를 출력하되, 국어 점수가 80점 이상만 출력해보자. db.Score.find({kor :{$gte :80}},{name:1,kor:1}); 5. Score에서 이름이 a로 시작되는 애들 출력. db.Score.find({name : /^a/}) 6. Score클래스에 이름 출력하되, 중복되지 않도록 출력하자. distinct(필드, 쿼리); db.Score.distinct("name"); 7. distinct를 이용해 영어 점수가 80점 이상인 학생의 이름. db.Score.distinct("name", {eng : {$gte :80}}) 8. 최대값, 최소값 sort() + limit() 영어 점수가 가장 높은 레코드를 출력. db.Score.find({}).sort({eng:-1}).limit(1); 9. 국어 점수가 가장 낮은 레코드를 출력. db.Score.find({}).sort({kor:1}).limit(1); 10. 5개 중 2개를 건너 뛰고 3줄을 출력. db.Score.find({}).skip(2).limit(3) 11. 총 5개 중에서 2개를 건너 뛰고 3줄을 출력하되, 수학점수를 내림차순으로 출력. db.Score.find({}).skip(2).limit(3).sort({mat:-1}) 12. 이름과 수학의 점수를 출력하되, 수학 70점 이상의 총점을 출력해보자. 수학이라는 과목을 그룹핑해서 총점을 구한다. - group({key, reduce, initial, [keyf,][cond,]finalize}) key => name reduce => function(cur, result){ result.mat += cur.mat; } cond => {mat :{$gte : 70} initial :{mat : 0} db.Score.group({ key : function(doc){ return {name :doc.a}; }, reduce : function(cur, result){ result.a += cur.mat; result.count++; }, cond : {mat :{$gte : 70}}, initial :{a : 0} }); 13. 이름과 수학의 점수를 출력하되, 수학 70점 이상의 평균을 출력해보자. 수학이라는 과목을 그룹핑해서 평균을 구한다. db.Score.group({ key:function(doc) { return {name:1}; }, reduce: function(cur, result) { result.total += cur.mat; result.count++; result.avg=result.total/result.count; }, cond: {mat:{$gte:70}}, initial: {total:0, count:0,avg:0} }); 14. 수학과 국어 두 개의 점수가 모두 70점을 넘는 점수는 추출해서 두 점수의 합을 구해라. ex) 수학: 80 국어: 80 합: 160 아래 두가지 방법 다 됨 db.Score.group( {keyf: function(doc){return {mat:doc.mat, kor:doc.kor}}, reduce: function(curr, result){result.total = curr.mat + curr.kor;}, initial:{total:0}, cond:{mat:{$gte:70}, kor:{$gte:70}}}); db.Score.group({ key:{name:1}, reduce: function(cur,result){ result.mat +=cur.mat; result.kor +=cur.kor; result.sum += cur.mat+cur.kor; }, cond:{mat:{$gte:70},kor:{$gte:70}}, initial:{mat:0,kor:0,sum:0}, }); 15. Score에서 kor, eng, mat의 각각의 합을 구하고 총합을 구해라. db.Score.group({ key:function(doc) { return {name:1}; }, reduce: function(cur,result){ result.mat +=cur.mat; result.eng +=cur.eng; result.kor +=cur.kor; result.sum += cur.mat+cur.kor+cur.eng; }, cond:{}, initial:{mat:0,kor:0,eng:0,sum:0}, }); 16. map=function(){ for(var key in this){ emit(key,{count:1}); } } reduce = function(key,emits){ total=0; for(var i in emits){ total += emits[i].count; } return {"count":total}; } db.Score.mapReduce(map,reduce,{out:{inline:1}}) ---------------------------- m = function(){ emit(this.name, this.kor); } r = function(key,kor){ return Array.sum(kor); }; db.Score.mapReduce(m,r,{out:{inline:1}}) 1.이름과 영어점수를 출력하되 영어점수의 합을 구하라. db.Score.mapReduce( function(){emit(this.name, this.mat)}, function(key, mat){return Array.sum(mat) }, {out:{inline:1}}) 문제1 : Person컬렉션에 Book칼럼을 3개이상 대입한 후에 Book java, 20000 Book java, 40000 Book spring , 40000 Book spring , 30000 ================ java 60000 spring 70000
관련 URL: http://docs.mongodb.org/manual/reference/method/db.collection.group/