{"id":779,"date":"2021-11-04T03:04:19","date_gmt":"2021-11-04T03:04:19","guid":{"rendered":"https:\/\/scrutineer.co.kr\/wordpress\/?p=779"},"modified":"2021-11-04T04:37:57","modified_gmt":"2021-11-04T04:37:57","slug":"%eb%aa%bd%ea%b3%a0%eb%94%94%eb%b9%84-%ec%9d%b8-%ec%95%a1%ec%85%98-6%ec%9e%a5-1","status":"publish","type":"post","link":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/2021\/11\/04\/%eb%aa%bd%ea%b3%a0%eb%94%94%eb%b9%84-%ec%9d%b8-%ec%95%a1%ec%85%98-6%ec%9e%a5-1\/","title":{"rendered":"\ubabd\uace0\ub514\ube44 \uc778 \uc561\uc158 6\uc7a5 -2"},"content":{"rendered":"\n<p class=\"has-medium-font-size wp-block-paragraph\">\ub3c4\ud050\uba3c\ud2b8 \uc7ac\uad6c\uc131<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB \uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778\uc740 \ub3c4\ud050\uba3c\ud2b8\ub97c \ubcc0\ud615\ud558\uc5ec \ucd9c\ub825 \ub3c4\ud050\uba3c\ud2b8\ub97c \uc0dd\uc131\ud558\ub294 \ub370 \uc0ac\uc6a9\ud560 \uc218 \uc788\ub294 \ub9ce\uc740 \ud568\uc218\ub97c \uac00\uc9c0\uace0 \uc788\ub2e4.<br>\uc77c\ubc18\uc801\uc73c\ub85c $project \uc5f0\uc0b0\uc790\uc640 \uc0ac\uc6a9\ub418\uc9c0\ub9cc $group \uc5f0\uc0b0\uc790\uc5d0 \ub300\ud55c _id\ub97c \uc815\uc758\ud560 \ub54c \uc0ac\uc6a9\ud560 \uc218\ub3c4 \uc788\uc74c.<br>(\uc9d1\uacc4 \ud504\ub808\uc784\uc6cc\ud06c \uc7ac\ud615\uc131 \ud568\uc218 \ubaa9\ub85d : https:\/\/docs.mongodb.com\/manual\/reference\/operator\/aggregation\/group\/)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">db.users.aggregate([     \n     {$match: {username: 'kbanker'}},     \n    \/\/first, last \ub450\uac1c\uc758 \ud544\ub4dc\uac00 \uc788\ub294 name \ud558\uc704 \uac1d\uccb4 \uc0dd\uc131\n     {$project: {name: {first:'$first_name',        \n                             last:'$last_name'}}\n     } ])\n\n{ \"_id\" : ObjectId(\"4c4b1476238d3b4dd5000001\"),\n      \"name\" : { \"first\" : \"Kyle\",\n                 \"last\" : \"Banker\" }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\ubb38\uc790\uc5f4\ud568\uc218<ul><li>$concat : \ub450 \uac1c \uc774\uc0c1\uc758 \ubb38\uc790\uc5f4\uc744 \ub2e8\uc77c \ubb38\uc790\uc5f4\ub85c \uc5f0\uacb0 <\/li><li>$strcasecmp : \ub300\/\uc18c\ubb38\uc790\ub97c \uad6c\ubd84\ud558\uc9c0 \uc54a\ub294 \ubb38\uc790\uc5f4 \ube44\uad50\ub97c \ud558\uba70, \uc22b\uc790 \ubc18\ud658<\/li><li>$substr : \ubb38\uc790\uc5f4\uc758 \ubd80\ubd84 \ubb38\uc790\uc5f4 \uc0dd\uc131 (v3.4 deprecated, $substrBytes \ub300\uccb4)<\/li><li>$toLower : \uc18c\ubb38\uc790 \ubcc0\ud658<\/li><li>$toUpper : \ub300\ubb38\uc790 \ubcc0\ud658<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">db.users.aggregate([\n     {$match: {username: 'kbanker'}},\n     {$project: \n         \/\/first_name\uacfc last_name\uc744 \uacf5\ubc31\uc73c\ub85c \uc5f0\uacb0\n        {name: {$concat:['$first_name', ' ', '$last_name']},    \n                  \/\/firstInital\uc740 \uc774\ub984\uc758 \uccab \ubc88\uc9f8 \ubb38\uc790\ub85c \uc124\uc815      \n                  firstInitial: {$substr: ['$first_name',0,1]},\n                  \/\/\uc0ac\uc6a9\uc790 \uc774\ub984\uc740 \ub300\ubb38\uc790\ub85c \ubcc0\uacbd            \n                  usernameUpperCase: {$toUpper: '$username'}  \n          }\n     }\n ])\n\n{ \"_id\" : ObjectId(\"4c4b1476238d3b4dd5000001\"),\n  \"name\"  :  \"Kyle  Banker\",\n  \"firstInitial\"  :  \"K\",\n  \"usernameUpperCase\" : \"KBANKER\" \n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\uc0b0\uc220 \ud568\uc218<ul><li>$add: \ubc30\uc5f4\ubc88\ud638\ub97c \ucd94\uac00<\/li><li>$divide: \uccab \ubc88\uc9f8 \uc22b\uc790\ub97c \ub450 \ubc88\uc9f8 \uc22b\uc790\ub85c \ub098\ub214<\/li><li>$mod: \uccab \ubc88\uc9f8 \uc22b\uc790\uc758 \ub098\uba38\uc9c0\ub97c \ub450 \ubc88\uc9f8 \uc22b\uc790\ub85c \ub098\ub214<\/li><li>$multiply: \uc22b\uc790 \ubc30\uc5f4\uc744 \uacf1\ud568<\/li><li>$subtract: \uccab \ubc88\uc9f8 \uc22b\uc790\uc5d0\uc11c \ub450 \ubc88\uc9f8 \uc22b\uc790\ub97c \ube8c<\/li><\/ul><\/li><li>\ub0a0\uc9dc\/\uc2dc\uac04 \ud568\uc218<ul><li>$dayOfYear: \uc5f0 \uc911\uc758 \uc77c\ub85c\uc11c 1 ~ 366<\/li><li>$dayOfMonth : \uc6d4 \uc911\uc758 \uc77c\ub85c\uc11c 1 ~ 31<\/li><li>$dayOfWeek : \uc8fc \uc911\uc758 \uc77c\ub85c\uc11c 1 ~ 7 (1: \uc77c\uc694\uc77c)<\/li><li>$year: \ub0a0\uc9dc\uc758 \uc5f0\ub3c4<\/li><li>$month: \ub0a0\uc9dc\uc758 \ub2ec (1 ~ 12)<\/li><li>$week: \ub0a0\uc9dc\uc758 \uc8fc (0 ~ 53)<\/li><li>$hour: \uc2dc\uac04 (0 ~ 23)<\/li><li>$minute: \ubd84 (0 ~ 59)<\/li><li>$secont: \ucd08 (0 ~ 59)<\/li><li>$millisecond: \uc2dc\uac04 \uc911 \ubc00\ub9ac\ucd08 (0~ 999)<\/li><\/ul><\/li><li>\ub17c\ub9ac \ud568\uc218<ul><li>$and: \ubaa8\ub4e0 \uac12\uc774 true\uc77c \uacbd\uc6b0 true<\/li><li>$cmp: \ub450 \uac1c \uac12\uc744 \ube44\uad50\ud558\uc5ec \uacb0\uacfc \ubc18\ud658, \ub450 \uac12\uc774 \ub3d9\uc77c\ud558\uba74 0<\/li><li>$cond: if .. then .. else \uc870\uac74\ubd80 \ub17c\ub9ac<\/li><li>$eq: \ub450 \uac12\uc774 \ub3d9\uc77c\ud558\uba74 true<\/li><li>$gt: ><\/li><li>$gte: >=<\/li><li>$ifNull: null\uc77c \uacbd\uc6b0 true<\/li><li>$lt: &lt;<\/li><li>$lte: &lt;=<\/li><li>$ne: \ub450 \uac12\uc774 \ub3d9\uc77c\ud558\uc9c0 \uc54a\uc73c\uba74 true<\/li><li>$not: \uc8fc\uc5b4\uc9c4 \uac12\uc758 \ubc1b\ub300 \uc870\uac74 \ubc18\ud658, true\uba74 false, false\uba74 true<\/li><li>$or: \uc5b4\ub5a4 \ud558\ub098\ub77c\ub3c4 true\uba74 true<\/li><\/ul><\/li><li>\uc9d1\ud568 \ud568\uc218<ul><li>$setEquals: \ub450 \uac1c\uc758 \uc9d1\ud569\uc774 \uc644\uc804\ud788 \uac19\uc740 \uc694\uc18c\ub97c \uac00\uc9c0\uba74 true <\/li><li>$setIntersection: \ub450 \uac1c\uc758 \uc9d1\ud569\uc5d0\uc11c \uacf5\ud1b5\uc801\uc73c\ub85c \uc874\uc7ac\ud558\ub294 \uc694\uc18c \ubc30\uc5f4 \ubc18\ud658<\/li><li>$setDifference: \ub450 \ubc88\uc9f8 \uc9d1\ud569\uc5d0 \uc5c6\ub294 \uccab \ubc88\uc9f8 \uc9d1\ud569\uc758 \uc694\uc18c \ubc18\ud658<\/li><li>$setUnion: \ub450 \uc9d1\ud569\uc758 \ud569\uc9d1\ud569<\/li><li>$setIsSubset: \ub450 \ubc88\uc9f8 \uc9d1\ud569\uc774 \uccab \ubc88\uc9f8 \uc9d1\ud569\uc758 \ubd80\ubd84\uc9d1\ud569\uc774\uba74 true <\/li><li>$anyElementTrue: \uc9d1\ud569\uc758 \uc694\uc18c \uc911 \ud558\ub098\ub77c\ub3c4 true\uba74 true<\/li><li>$allElementsTrue: \uc9d1\ud569\uc758 \ubaa8\ub4e0 \uc694\uc18c\uac00 true\uba74 true<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">{ \"_id\" : ObjectId(\"4c4b1476238d3b4dd5003981\"),\n   \"productName\" : \"Extra Large Wheel Barrow\",\n   \"tags\" : [ \"tools\", \"gardening\", \"soil\" ]}\n{ \"_id\" : ObjectId(\"4c4b1476238d3b4dd5003982\"),\n \"productName\"  :  \"Rubberized  Work  Glove,  Black\", \n  \"tags\" : [ \"gardening\" ]}\n\n\ntestSet1 = ['tools']   \ndb.products.aggregate([\n     {$project:\n         {productName: '$name',\n          tags:1,\n          setUnion: {$setUnion:['$tags',testSet1]}, \/\/ $setUnion \uc73c\ub85c tags(=tools) \uacb0\ud569\n        }\n     }\n ])\n\n\n\n{   \"_id\" : ObjectId(\"4c4b1476238d3b4dd5003981\"),\n     \"productName\" : \"Extra Large Wheel Barrow\",\n     \"tags\" : [\"tools\", \"gardening\", \"soil\"],     \/\/tools, gardening, soil \uc870\ud569\n     \"setUnion\" : [\"gardening\",\"tools\",\"soil\"]\n} \n{   \"_id\" : ObjectId(\"4c4b1476238d3b4dd5003982\"),\n     \"productName\" : \"Rubberized Work Glove, Black\",\n     \"tags\" : [\"gardening\"],\n     \"setUnion\" : [\"tools\", \"gardening\"]       \/\/tolls,gardening \uc870\ud569\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\uae30\ud0c0 \ud568\uc218<ul><li>$meta: \ud14d\uc2a4\ud2b8 \uac80\uc0c9 \uad00\ub828 \uc815\ubcf4\uc5d0 \uc811\uadfc<\/li><li>$size: \ubc30\uc5f4\uc758 \ud06c\uae30 \ubc18\ud658 => \ubc30\uc5f4\uc774 \ube44\uc5b4\uc788\ub294\uc9c0 \uc5ec\ubd80 \ud655\uc778\uc5d0 \uc720\uc6a9<\/li><li>$map: \ubc30\uc5f4\uc758 \uac01 \uba64\ubc84\uc5d0 \ud45c\ud604\uc2dd(expression) \uc801\uc6a9 => $unwind\ub97c \uc0ac\uc6a9\ud558\uc9c0 \uc54a\uace0 \ubc30\uc5f4\uc758 \ub0b4\uc6a9\uc744 \ubc14\uafb8\uace0 \uc2f6\uc744 \ub54c \uc720\uc6a9.<\/li><li>$let: \ud45c\ud604\uc2dd\uc758 \ubc94\uc704 \ub0b4\uc5d0\uc11c \uc0ac\uc6a9\ub418\ub294 \ubcc0\uc218 \uc815\uc758 => \uc5ec\ub7ec \uac1c\uc758 $project \ub2e8\uacc4\ub97c \uc0ac\uc6a9\ud558\uc9c0 \uc54a\uace0 \uc784\uc2dc \uc815\uc758 \ubcc0\uc218 \uc0ac\uc6a9 \uac00\ub2a5.<\/li><li>$literal: \ud45c\ud604\uc2dd\uc758 \uac12\uc744 \ud3c9\uac00\ud558\uc9c0 \uc54a\uace0 \ubc18\ud658 => \ud544\ub4dc\uac12\uc744 0,1 \ub610\ub294 $\ub85c \ucd08\uae30\ud654\ud560 \ub54c \ubc1c\uc0dd\ud558\ub294 \ubb38\uc81c\ub97c \ud53c\ud560 \uc218 \uc788\uc74c<\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">\uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778 \uc131\ub2a5\uc5d0 \ub300\ud55c \uc774\ud574<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778 \uc131\ub2a5\uc5d0 \uc911\uc694\ud55c \uc601\ud5a5\uc744 \ubbf8\uce60 \uc218 \uc788\ub294 \uc0ac\ud56d<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\ud30c\uc774\ud504\ub77c\uc778\uc5d0\uc11c \uac00\ub2a5\ud55c \ud55c \ube68\ub9ac \ub3c4\ud050\uba3c\ud2b8 \uc218\uc640 \ud06c\uae30\ub97c \uc904\uc778\ub2e4.<\/li><li>\uc778\ub371\uc2a4\ub294 $match, $sort \uc5d0\uc11c\ub9cc \uc0ac\uc6a9 \uac00\ub2a5<\/li><li>sharding\uc744 \uc0ac\uc6a9\ud558\ub294 \uacbd\uc6b0(\ub9e4\uc6b0 \ud070 \uceec\ub809\uc158\uc758 \uacbd\uc6b0) $match \ubc0f $project \uc5f0\uc0b0\uc790\ub294 \uac1c\ubcc4 \uc0e4\ub4dc\uc5d0\uc11c \uc2e4\ud589\ub41c\ub2e4. \ub2e4\ub978 \uc5f0\uc0b0\uc790\ub97c \uc0ac\uc6a9\ud558\uba74 \ub0a8\uc544\uc788\ub294 \ud30c\uc774\ud504\ub77c\uc778\uc774 \ud504\ub77c\uc774\uba38\ub9ac \uc0e4\ub4dc\uc5d0\uc11c \uc2e4\ud589\ub41c\ub2e4.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778\uc758 \uc635\uc158<br>aggregate() \ud568\uc218\uc5d0 \uc804\ub2ec\ud560 \uc218 \uc788\ub294 \ub450 \ubc88\uc9f8 \ub9e4\uac1c\ubcc0\uc218 \uc635\uc158<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>explain() &#8211; \ud30c\uc774\ud504\ub77c\uc778\uc744 \uc2e4\ud589\ud558\uace0 \uc624\uc9c1 \ud30c\uc774\ud504\ub77c\uc778 \ud504\ub85c\uc138\uc2a4 \uc138\ubd80 \uc815\ubcf4\ub9cc \ubc18\ud658<\/li><li>allowDiskUse &#8211; \uc911\uac04 \uacb0\uacfc\ub97c \uc704\ud574 \ub514\uc2a4\ud06c\ub97c \uc0ac\uc6a9<\/li><li>cursor &#8211; \ucd08\uae30 \ubc30\uce58 \ud06c\uae30 \uc9c0\uc815<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/\/\uc635\uc158 \uc0ac\uc6a9 \ud615\uc2dd\ndb.collection.aggregate(pipeline,additionalOptions) \/\/ additionalOptions : aggregate()\uc5d0 \uc804\ub2ec\ud560 \uc218 \uc788\ub294 \uc120\ud0dd\uc801 JSON \uac1d\uccb4\n\n\/\/additionalOptions \ud615\uc2dd\n{explain:true, allowDiskUse:true, cursor: {batchSize: n} }<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778\uc758 explain() \ud568\uc218<\/p><cite>SQL\uc5d0\uc11c\uc758 explain\uacfc \uc720\uc0ac\ud558\ub2e4. \uac1c\ubc1c\uc790\uac00 \ucffc\ub9ac\uc5d0\uc11c \uc0ac\uc6a9\ud55c \uc778\ub371\uc2a4\ub97c \ubc1d\ud600 \ub0c4\uc73c\ub85c\uc368 \ub290\ub9b0 \uc5f0\uc0b0\uc744 \uc9c4\ub2e8\ud560 \uc218 \uc788\ub2e4.<\/cite><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">> db.numbers.find({num: {\"$gt\": 19995 }}).explain(\"executionStats\")\n\n {\n     \"queryPlanner\" : {\n         \"plannerVersion\" : 1,\n         \"namespace\" : \"tutorial.numbers\",\n         \"indexFilterSet\" : false,\n         \"parsedQuery\" : {\n             \"num\" : {\n                 \"$gt\" : 19995\n             }\n         },\n        \"winningPlan\" : {\n            \"stage\" : \"FETCH\",\n            \"inputStage\" : {\n                \"stage\" : \"IXSCAN\",\n                \"keyPattern\" : {\n                    \"num\" : 1\n                },\n                \"indexName\" : \"num_1\", \/\/num_1 \uc778\ub371\uc2a4\ub97c \uc0ac\uc6a9\n                \"isMultiKey\" : false,\n                \"direction\" : \"forward\",\n                \"indexBounds\" : {\n                    \"num\" : [\n                        \"(19995.0, inf.0]\"\n                    ]\n                }\n            }\n        },\n        \"rejectedPlans\" : [ ]\n    },\n    \"executionStats\" : {\n        \"executionSuccess\" : true,\n        \"nReturned\" : 4,                \/\/4\uac1c\uc758 \ub3c4\ud050\uba3c\ud2b8\uac00 \ubc18\ud658\n        \"executionTimeMillis\" : 0,    \/\/ \ud6e8\uc52c \ub354 \ube60\ub984\n        \"totalKeysExamined\" : 4,\n        \"totalDocsExamined\" : 4,    \/\/4\uac1c\uc758 \ub3c4\ud050\uba3c\ud2b8\ub9cc \uc2a4\uce94\n            \"executionStages\" : {\n              \"stage\" : \"FETCH\",\n              \"nReturned\" : 4,\n              \"executionTimeMillisEstimate\" : 0, \/\/\ud6e8\uc52c \ub354 \ube60\ub984\n              \"works\" : 5,\n              \"advanced\" : 4,\n              \"needTime\" : 0,\n              \"needFetch\" : 0, \n              \"saveState\" : 0,\n              \"restoreState\" : 0,\n              \"isEOF\" : 1,\n              \"invalidates\" : 0,\n              \"docsExamined\" : 4,\n              \"alreadyHasObj\" : 0,\n              \"inputStage\" : {\n                 \"stage\" : \"IXSCAN\",\n                 \"nReturned\" : 4,\n                 \"executionTimeMillisEstimate\" : 0, \/\/\ud6e8\uc52c \ub354 \ube60\ub984\n                 \"works\" : 4,\n                 \"advanced\" : 4,\n                 \"needTime\" : 0,\n                 \"needFetch\" : 0,\n                 \"saveState\" : 0,\n                 \"restoreState\" : 0,\n                 \"isEOF\" : 1,\n                 \"invalidates\" : 0,\n                 \"keyPattern\" : {\n                    \"num\" : 1\n                 },\n                 \"indexName\" : \"num_1\",            \/\/num_1 \uc778\ub371\uc2a4 \uc0ac\uc6a9\n                 \"isMultiKey\" : false,\n                 \"direction\" : \"forward\",\n                 \"indexBounds\" : { \n                    \"num\" : [\n                         \"(19995.0, inf.0]\"\n                     ]\n                 },\n                 \"keysExamined\" : 4,\n                 \"dupsTested\" : 0,\n                 \"dupsDropped\" : 0,\n                 \"seenInvalidated\" : 0,\n                 \"matchTested\" : 0\n             }\n         }\n     },\n \"serverInfo\"  :  {\n         \"host\" : \"rMacBook.local\",\n         \"port\" : 27017, \n         \"version\"  :  \"3.0.6\",\n         \"gitVersion\" : \"nogitversion\" \n    },\n     \"ok\" : 1\n   }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">> countsByRating = db.reviews.aggregate([\n ...\n    {$match  :  {'product_id':  product['_id']}}, \/\/ $match \uba3c\uc800 \uc218\ud589\n ...  {$group : { _id:'$rating',\n ...            count:{$sum:1}}}\n ... ],{explain:true})                                  \/\/explain \uc635\uc158 true\n\n\n{\n    \"stages\" : [\n         {\n             \"$cursor\" : { \n                \"query\" : {\n                         \"product_id\" : ObjectId(\"4c4b1476238d3b4dd5003981\")\n                 },\n                 \"fields\" : {\n                         \"rating\" : 1,\n                         \"_id\" : 0\n                 },\n                 \"plan\" : {\n                      \"cursor\" : \"BtreeCursor \",    \/\/\uc778\ub371\uc2a4 \uae30\ubc18 \ucee4\uc11c\uc778 BTreeCursor \uc0ac\uc6a9\n                       \"isMultiKey\" : false,\n                       \"scanAndOrder\" : false,\n                       \"indexBounds\" : {\n                       \"product_id\" : [                \/\/\ub2e8\uc77c \uc81c\ud488\uc5d0 \uc0ac\uc6a9\ub418\ub294 \ubc94\uc704\n                               [\n                                 ObjectId(\"4c4b1476238d3b4dd5003981\"),\n                                 ObjectId(\"4c4b1476238d3b4dd5003981\")\n                             ]\n                         ]\n                     },\n                     \"allPlans\" : [\n                         ... \n                    ]\n             \"$group\" : { \n                \"_id\" : \"$rating\",\n                 \"count\" : {\n                         \"$sum\" : {\n                                 \"$const\" : 1\n                         }\n                 }\n             }\n         }\n     ],\n     \"ok\" : 1 }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\uc778\ub371\uc2a4\uac00 \uc0ac\uc6a9\ub418\uc5c8\ub294\uc9c0 \uadf8\ub9ac\uace0 \uc778\ub371\uc2a4 \ub0b4\uc5d0\uc11c \ubc94\uc704 \uc2a4\uce94\uc774 \ub418\uc5c8\ub294\uc9c0 \uc5ec\ubd80 \ud655\uc778<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>allowDiskUse \uc635\uc158<\/p><cite>\uc77c\ubc18\uc801\uc73c\ub85c allowDiskUse \uc635\uc158\uc744 \uc0ac\uc6a9\ud558\uba74 \ud30c\uc774\ud504\ub77c\uc778 \uc18d\ub3c4\uac00 \ub290\ub824\uc9c8 \uc218 \uc788\ub2e4.<\/cite><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">assert: command failed: {\n         \"errmsg\" : \"exception: Exceeded memory limit for $group,\n         but didn't allow  external sort. Pass allowDiskUse:true to opt in.\",\n         \"code\" : 16945,\n         \"ok\" : 0 \n} : aggregate failed<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud30c\uc774\ud504\ub77c\uc778 \ub2e8\uacc4\uc5d0\uc11c MongoDB\uac00 \ud5c8\uc6a9\ud558\ub294 100MB \ub7a8 \uc81c\ud55c\uc744 \ucd08\uacfc\ud560 \ub54c \uc624\ub958 \uba54\uc2dc\uc9c0.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">db.orders.aggregate([\n     {$match: {purchase_data: {$gte: new Date(2010, 0, 1)}}}, \/\/\ucc98\ub9ac\ud560 \ub3c4\ud050\uba3c\ud2b8\ub97c \uc904\uc774\uae30 \uc704\ud574 $match \uba3c\uc800 \uc0ac\uc6a9\n     {$group: {\n         _id: {year : {$year :'$purchase_data'},\n               month: {$month :'$purchase_data'}},\n         count: {$sum:1},\n         total: {$sum:'$sub_total'}}},\n     {$sort: {_id:-1}}\n ], {allowDiskUse:true});      \/\/MongoDB\uac00 \uc911\uac04 \uc800\uc7a5\uc744 \uc704\ud574 \ub514\uc2a4\ud06c\ub97c \uc0ac\uc6a9\ud558\uac8c \ud574\uc90c<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\uc9d1\uacc4 \ucee4\uc11c \uc635\uc158<\/p><cite>V2.6\uc774\uc804\uc5d0\ub294 \ud30c\uc774\ud504\ub77c\uc778\uc758 \uacb0\uacfc\uac00 16MB \uc81c\ud55c\ub41c \ud55c \uac1c\uc758 \ub3c4\ud050\uba3c\ud2b8\uc600\uc73c\ub098 V2.6 \uc774\ud6c4 \uae30\ubcf8\uac12\uc740 \ucee4\uc11c\ub97c \ubc18\ud658\ud558\ub294 \uac83\uc774\ub2e4.<br>\ub9ce\uc740 \uc591\uc758 \ub370\uc774\ud130\ub97c \uc2a4\ud2b8\ub9ac\ubc0d\ud560 \uc218 \uc788\uac8c \ud574\uc90c (toArray(), pretty() \uba54\uc11c\ub4dc \uc0ac\uc6a9 \uc9c0\uc591)<br>$group \ud30c\uc774\ud504\ub77c\uc778 \uc5f0\uc0b0\uc790\ub97c \uc0ac\uc6a9\ud558\uc5ec \uac01\uac01\uc744 \ud504\ub85c\uadf8\ub7a8\uc5d0 \ubcf4\ub0b4\uc9c0 \uc54a\uace0 \ucd9c\ub825 \ub3c4\ud050\uba3c\ud2b8\ub97c \uacc4\uc0b0\ud560 \uc218 \uc788\ub2e4.<\/cite><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">countsByRating  =  db.reviews.aggregate([\n      {$match : {'product_id': product['_id']}},\n      {$group : { _id:'$rating',\n            count:{$sum:1}}}\n ],{cursor:{}})   \/\/ \ucee4\uc11c\ub97c \ubc18\ud658<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>cursor.hasNext(): \uacb0\uacfc\uc5d0 \ub2e4\uc74c \ub3c4\ud050\uba3c\ud2b8\uac00 \uc874\uc7ac\ud558\ub294\uc9c0 \ud655\uc778<\/li><li>cursor.next(): \uacb0\uacfc\uc5d0\uc11c \ub2e4\uc74c \ub3c4\ud050\uba3c\ud2b8 \ubc18\ud658<\/li><li>cursor.toArray(): \uc804\uccb4 \uacb0\uacfc\ub97c \ubc30\uc5f4\ub85c \ubc18\ud658<\/li><li>cursor.forEach(): \uacb0\uacfc\uc758 \uac01 \ud589\uc5d0 \ub300\ud574 \ud568\uc218 \uc2e4\ud589<\/li><li>cursor.map(): \uacb0\uacfc\uc758 \uac01 \ud589\uc5d0 \ub300\ud574 \ud568\uc218 \uc2e4\ud589\ud558\uace0, \ud568\uc218 \ubc18\ud658\uac12\uc758 \ubc30\uc5f4\uc744 \ubc18\ud658<\/li><li>cursor.itcount(): \ud56d\ubaa9\uc218\ub97c \ubc18\ud658 (\ud14c\uc2a4\ud2b8 \uc804\uc6a9)<\/li><li>cursor.pretty(): \ud615\uc2dd\uc744 \uac16\ucd98 \uacb0\uacfc\uc758 \ubc30\uc5f4 \ud45c\uc2dc<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\uae30\ud0c0 \uc9d1\uacc4 \uae30\ub2a5<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>.count()<br>product = db.products.findOne({&#8216;slug&#8217;: &#8216;wheelbarrow-9092&#8217;})<br>reviews_count = db.reviews.count({&#8216;product_id&#8217;: product[&#8216;_id&#8217;]}) \/\/\uc81c\ud488\uc5d0 \ub300\ud55c \ub9ac\ubdf0 \uc9d1\uacc4<\/li><li>.distinct() : \ub3c4\ud050\uba3c\ud2b8 \ucd5c\ub300 \ud06c\uae30\uc778 16MB\ub85c \uc81c\ud55c \ub428<br>db.orders.distinct(&#8216;shipping_address.zip&#8217;)<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>\ub9f5\ub9ac\ub4c0\uc2a4 : \uc77c\ubc18\uc801\uc73c\ub85c \uc9d1\uacc4 \ud504\ub808\uc784\uc6cc\ud06c\ubcf4\ub2e4 \ud6e8\uc52c \ub290\ub9bc. \uadf8\ub7ec\ub098 \uc720\uc5f0\ud568.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/\/ \ud310\ub9e4 \uc694\uc57d \uc815\ubcf4 \uc81c\uacf5 \uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778\ndb.orders.aggregate([\n     {\"$match\": {\"purchase_data\":{\"$gte\" : new Date(2010, 0, 1)}}},\n     {\"$group\": {\n         \"_id\": {\"year\" : {\"$year\" :\"$purchase_data\"},\n                \"month\" : {\"$month\" : \"$purchase_data\"}},\n         \"count\": {\"$sum\":1},\n         \"total\": {\"$sum\":\"$sub_total\"}}},\n     {\"$sort\": {\"_id\":-1}}]);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ub9f5\ub9ac\ub4c0\uc2a4\ub97c \uc774\uc6a9\ud558\uc5ec \uc544\ub798\uc640 \uac19\uc774 \ud45c\ud604<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/\/ 1. map \ud568\uc218\ub97c \uc791\uc131\ud558\uc5ec \uadf8\ub8f9\ud654 \uc911\uc778 \ud0a4\ub97c \uc815\uc758\ud558\uace0 \uacc4\uc0b0\uc5d0 \ud544\uc694\ud55c \ubaa8\ub4e0 \ub370\uc774\ud130\ub97c \ud328\ud0a4\uc9c0\ud654\ud568\nmap  =  function()  {\n     var shipping_month = (this.purchase_data.getMonth()+1) +   \/\/\uc8fc\ubb38 \uc0dd\uc131\ub41c \uc6d4\n         '-' + this.purchase_data.getFullYear();\n\n     var tmpItems = 0;\n     this.line_items.forEach(function(item) {\n          tmpItems  +=  item.quantity;\n     });\n\n    \/\/emit() \ud568\uc218 \uc774\uc6a9, \uccab \ubc88\uc9f8 \uc778\uc218\ub294 \uadf8\ub8f9\ud654 \uae30\uc900, \ub450 \ubc88\uc9f8 \uc778\uc218\ub294 \uc77c\ubc18\uc801\uc73c\ub85c \uac12\uc744 \uc904\uc774\ub294 \ub3c4\ud050\uba3c\ud2b8\n     emit(shipping_month, {order_total: this.sub_total,         \n                                   items_total: tmpItems});\n };\n\n\/\/ 2. \ub300\uc751\ud558\ub294 reduce \ud568\uc218\ub294 \uc774\ub97c \ub354 \uba85\ud655\ud558\uac8c \ub9cc\ub4e4\uc5b4\uc57c\ud568\n\/\/ reduce \uc791\uc5c5\uc740 \ud574\ub2f9 \uac12\uc774 \uc6d0\ud558\ub294 \ubc29\uc2dd\uc73c\ub85c \ud568\uacc4 \uc9d1\uacc4\ub41c \ub2e4\uc74c \ub2e8\uc77c \uac12\uc73c\ub85c \ubc18\ud658\ub418\ub294 \uc9c0 \ud655\uc778.\n\/\/ \ub450\ubc88 \uc774\uc0c1 \ud638\ucd9c\ub420 \uc218 \uc788\uc74c.\nreduce = function(key, values) {   \/\/\ub9ac\ub4c0\uc2a4 \ud568\uc218\uc5d0\ub294 \ud0a4\uc640 \ud558\ub098 \uc774\uc0c1\uc758 \uac12\uc758 \ubc30\uc5f4\uc774 \uc804\ub2ec\n     var result = { order_total: 0, items_total: 0 };\n     values.forEach(function(value){\n              result.order_total  +=  value.order_total;\n              result.items_total  +=  value.items_total;\n     });\n     return ( result );\n };<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ucffc\ub9ac \ud544\ud130\ub97c \ucd94\uac00\ud558\uace0 \uacb0\uacfc\ub97c \uc800\uc7a5\ud558\uae30<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/\/ filter\ub294 \ud2b9\uc815 \uc8fc\ubb38\ub9cc \uc120\ud0dd\n\/\/ \ub9f5\uc740 \ud0a4-\uac12 \uc30d\uc744 \ub0b4\ubcf4\ub0b4\uba70, \uc77c\ubc18\uc801\uc73c\ub85c \uac01\uac01 \ud558\ub098\uc529 \ucd9c\ub825\n\/\/ \ub9ac\ub4c0\uc2a4\ub294 \ub9f5\uc5d0 \uc758\ud574 \uc0dd\uc131\ub41c \uac12 \ubc30\uc5f4\uc744 \uc804\ub2ec\ubc1b\uace0 \ucd9c\ub825\nfilter = {purchase_data: {$gte: new Date(2010, 0, 1)}};\ndb.orders.mapReduce(map,  reduce,  {query:  filter,  out:  'totals'});  \/\/totals\ub77c\ub294 \uceec\ub809\uc158\uc5d0 \uacb0\uacfc \uc800\uc7a5\n\n\/\/ \ub9f5\ub9ac\ub4c0\uc2a4 \uacb0\uacfc \uceec\ub809\uc158 \uc870\ud68c\n> db.totals.find()\n { \"_id\" : \"11-2014\", \"value\" : {\"order_total\" : 4897, \"items_total\" : 1 } }\n { \"_id\" : \"4-2014\", \"value\" : {\"order_total\" : 4897, \"items_total\" : 1 } }\n { \"_id\" : \"8-2014\", \"value\" : {\"order_total\" : 11093, \"items_total\" : 4 } }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ub3c4\ud050\uba3c\ud2b8 \uc7ac\uad6c\uc131 MongoDB \uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778\uc740 \ub3c4\ud050\uba3c\ud2b8\ub97c \ubcc0\ud615\ud558\uc5ec \ucd9c\ub825 \ub3c4\ud050\uba3c\ud2b8\ub97c \uc0dd\uc131\ud558\ub294 \ub370 \uc0ac\uc6a9\ud560 \uc218 \uc788\ub294 \ub9ce\uc740 \ud568\uc218\ub97c \uac00\uc9c0\uace0 \uc788\ub2e4.\uc77c\ubc18\uc801\uc73c\ub85c $project \uc5f0\uc0b0\uc790\uc640 \uc0ac\uc6a9\ub418\uc9c0\ub9cc $group \uc5f0\uc0b0\uc790\uc5d0 \ub300\ud55c _id\ub97c \uc815\uc758\ud560 \ub54c \uc0ac\uc6a9\ud560 \uc218\ub3c4 \uc788\uc74c.(\uc9d1\uacc4 \ud504\ub808\uc784\uc6cc\ud06c \uc7ac\ud615\uc131 \ud568\uc218 \ubaa9\ub85d : https:\/\/docs.mongodb.com\/manual\/reference\/operator\/aggregation\/group\/) \ubb38\uc790\uc5f4\ud568\uc218 $concat : \ub450 \uac1c \uc774\uc0c1\uc758 \ubb38\uc790\uc5f4\uc744 \ub2e8\uc77c \ubb38\uc790\uc5f4\ub85c \uc5f0\uacb0 $strcasecmp : \ub300\/\uc18c\ubb38\uc790\ub97c \uad6c\ubd84\ud558\uc9c0 \uc54a\ub294 \ubb38\uc790\uc5f4 \ube44\uad50\ub97c \ud558\uba70, \uc22b\uc790 &#8230; <a title=\"\ubabd\uace0\ub514\ube44 \uc778 \uc561\uc158 6\uc7a5 -2\" class=\"read-more\" href=\"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/2021\/11\/04\/%eb%aa%bd%ea%b3%a0%eb%94%94%eb%b9%84-%ec%9d%b8-%ec%95%a1%ec%85%98-6%ec%9e%a5-1\/\" aria-label=\"Read more about \ubabd\uace0\ub514\ube44 \uc778 \uc561\uc158 6\uc7a5 -2\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-779","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=779"}],"version-history":[{"count":7,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/779\/revisions"}],"predecessor-version":[{"id":792,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/779\/revisions\/792"}],"wp:attachment":[{"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}