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