MongoDB

1 615 vues

Publié le

Publié dans : Technologie, Business
0 commentaire
3 j’aime
Statistiques
Remarques
  • Soyez le premier à commenter

Aucun téléchargement
Vues
Nombre de vues
1 615
Sur SlideShare
0
Issues des intégrations
0
Intégrations
82
Actions
Partages
0
Téléchargements
2
Commentaires
0
J’aime
3
Intégrations 0
Aucune incorporation

Aucune remarque pour cette diapositive

MongoDB

  1. 1. MongoDB Ch 6. Aggregation<br />아키텍트를 꿈꾸는 사람들cafe.naver.com/architect1<br />현수명 soomong.net<br />#soomong<br />
  2. 2. Count<br />Distinct<br />Group<br />MapReduce<br />
  3. 3. Count<br />> db.foo.find()<br />{ "_id" : ObjectId(“4e2…"), "bar" : "baz" }<br />{ "_id" : ObjectId(“4e2…"), "a" : "abc" }<br />{ "_id" : ObjectId(“4e2…"), "ohho" : "hohohohoho" }<br />{ "_id" : ObjectId(“4e2…"), "123456" : "gooood" }<br />> db.foo.count()<br />4<br />
  4. 4. Count<br />> db.foo.insert({"x":2})<br />> db.foo.count()<br />5<br />
  5. 5. Count+<br />> db.foo.count<br />function (x) {<br /> return this.find(x).count();<br />> db.foo.count = 0<br />0<br />> db.foo.count()<br />Mon Jul 25 00:13:19 TypeError: <br />db.foo.count is not a function (shell):1<br />
  6. 6. Distinct<br />> db.foo.find()<br />{ "_id" : ObjectId(“4e2…"), "bar" : "baz" }<br />{ "_id" : ObjectId(“4e2…"), "a" : "abc" }<br />{ "_id" : ObjectId(“4e2…"), "ohho" : "hohohohoho" }<br />{ "_id" : ObjectId(“4e2…"), "123456" : "gooood" }<br />> db.runCommand({"distinct":"foo","key":"ohho"})<br />{<br /> "values" : [<br /> "hohohohoho"<br /> ],<br /> "stats" : {<br /> "n" : 5,<br /> "nscanned" : 5,<br /> "nscannedObjects" : 5,<br /> "timems" : 1<br /> },<br /> "ok" : 1<br />}<br />
  7. 7. Distinct+<br />> db.runCommand<br />function (obj) {<br /> if (typeofobj == "string") {<br />var n = {};<br /> n[obj] = 1;<br />obj = n;<br /> }<br /> return this.getCollection("$cmd").findOne(obj);<br />}<br />> db.foo.runCommand<br />function (cmd, params) {<br /> if (typeofcmd == "object") {<br /> return this._db._dbCommand(cmd);<br /> }<br />var c = {};<br /> c[cmd] = this.getName();<br /> if (params) {<br />Object.extend(c, params);<br /> }<br /> return this._db._dbCommand(c);<br />}<br />
  8. 8. Group<br />> db.stocks.find()<br />{ "_id" : ObjectId(“…"), "day" : "2011/07/25", <br />"time" : “2011/07/25 01:00:00 GMT-400", "price" : 4.23 }<br />{ "_id" : ObjectId(“…"), "day" : "2011/07/26", <br />"time" : “2011/07/26 11:05:00 GMT-400", "price" : 4.27 }<br />{ "_id" : ObjectId(“…"), "day" : "2011/07/25", <br />"time" : “2011/07/25 03:47:00 GMT-400", "price" : 4.1 }<br />{ "_id" : ObjectId(“…"), "day" : "2011/07/28", <br />"time" : “2011/07/28 05:27:38 GMT-400", "price" : 4.3 }<br />{ "_id" : ObjectId(“…"), "day" : "2011/07/26", <br />"time" : “2011/07/26 06:34:50 GMT-400", "price" : 4.01 }<br />{"time" : “2011/07/25 03:47:00 GMT-400", "price" : 4.1 }<br />{"time" : “2011/07/26 11:05:00 GMT-400", "price" : 4.27 }<br />{"time" : “2011/07/28 05:27:38 GMT-400", "price" : 4.3 }<br />
  9. 9. Group<br />> db.runCommand({"group" : {<br />... "ns" : "stocks",<br />... "key" : "day",<br />... "initial" : {"time" : 0},<br />... "$reduce" : function(doc, prev) {<br />... if (doc.time > prev.time) {<br />... prev.price = doc.price;<br />... prev.time = doc.time;<br />... }<br />... }}})<br />{"time" : “2011/07/25 03:47:00 GMT-400", "price" : 4.1 }<br />{"time" : “2011/07/26 11:05:00 GMT-400", "price" : 4.27 }<br />{"time" : “2011/07/28 05:27:38 GMT-400", "price" : 4.3 }<br />
  10. 10. Group<br />"ns" : Collection<br />"key" : 묶고싶은 Key<br />"initial" : reduce 연산의 초기값<br />" $ , $reduce" : reduce 연산<br />"condition" : 조건<br />"finalize" : 마지막에 한번만 수행<br />"$keyf" : 키를 함수로<br />
  11. 11. MapReduce<br />문제를 분할<br />각 청크를 다른 서버로<br />각 서버가 처리후<br />결과 모으기<br />장점<br />쉽게 병렬화<br />단점<br />복잡타<br />
  12. 12. Map<br />컬렉션내에 각 키가 몇번씩 나타나는지 세어보자<br />map = function () {<br /> for (var key in this) {<br />emit(key, {count:1});<br /> }<br />
  13. 13. Reduce<br />reduce = function (key, emits) {<br /> total = 0;<br /> for (vari in emits) {<br />total += emits[i].count;<br /> }<br /> return {count:total};<br />
  14. 14. Execution<br />mr = db.foo.mapReduce<br />(map,reduce,{out:"mongoDBmapReduce"})<br />{<br /> "result" : "mongoDBmapReduce",<br /> "timeMillis" : 11,<br /> "counts" : {<br /> "input" : 4,<br /> "emit" : 8,<br /> "output" : 5<br /> },<br /> "ok" : 1,<br />}<br />input : 맵함수로 보내진 문서수<br />emit : 맵 함수에서 호출한 emit 수<br />Output : 결과 컬렉션에 생성된 문서수<br />
  15. 15.
  16. 16. MapReduce<br />Parallelism<br />As of right now, MapReduce jobs on a single mongod process are single threaded. <br />This is due to a design limitationin current JavaScript engines. <br />We are looking into alternatives to solve this issue, <br />but for now if you want to parallelize your MapReduce jobs, <br />you will need to either use shardingor <br />do the aggregation client-side in your code.<br />
  17. 17. Reference<br />MongoDB완벽 가이드<br />-한빛미디어<br />http://www.mongodb.org/<br />http://www.coolinfographics.com/<br />
  18. 18. 감사합니다<br />

×