{"id":762,"date":"2021-10-28T04:43:54","date_gmt":"2021-10-28T04:43:54","guid":{"rendered":"https:\/\/scrutineer.co.kr\/wordpress\/?p=762"},"modified":"2021-11-04T02:42:08","modified_gmt":"2021-11-04T02:42:08","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","status":"publish","type":"post","link":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/2021\/10\/28\/%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\/","title":{"rendered":"\ubabd\uace0\ub514\ube44 \uc778 \uc561\uc158 6\uc7a5 -1"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\" style=\"font-size:30px\">\uc9d1\uacc4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB \uc9d1\uacc4 \ud504\ub808\uc784\uc6cc\ud06c\ub97c \uc0ac\uc6a9\ud558\uc5ec \ubcf4\ub2e4 \ubcf5\uc7a1\ud55c \ucffc\ub9ac \uc0ac\uc6a9\uc744 \uc54c\uc544\ubcf8\ub2e4. (\uc608. \uc6d4\ubcc4\ub9e4\ucd9c, \uc81c\ud488\ubcc4 \ub9e4\ucd9c, \uc0ac\uc6a9\uc790\ubcc4 \uc8fc\ubb38\ud569\uacc4 \ub4f1, cf. GROUP BY)<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">\uc9d1\uacc4 \ud504\ub808\uc784\uc6cc\ud06c \uac1c\uc694<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778<ul><li>$project : \ucd9c\ub825 \ub3c4\ud050\uba3c\ud2b8\uc0c1\uc5d0 \ubc30\uce58\ud560 \ud544\ub4dc \uc9c0\uc815<\/li><li>$match : \ucc98\ub9ac\ub420 \ub3c4\ud050\uba3c\ud2b8\ub97c \uc120\ud0dd\ud558\ub294 \uac83. (= find())<\/li><li>$limit : \ub2e4\uc74c \ub2e8\uacc4\uc5d0 \uc804\ub2ec\ub420 \ub3c4\ud050\uba3c\ud2b8\uc758 \uc218 \uc81c\ud55c<\/li><li>$skip : \uc9c0\uc815\ub41c \uc218\uc758 \ub3c4\ud050\uba3c\ud2b8\ub97c \uac74\ub108\ub6f4\ub2e4.<\/li><li>$unwind : \ubc30\uc5f4\uc744 \ud655\uc7a5\ud558\uc5ec \uac01 \ubc30\uc5f4 \ud56d\ubaa9\uc5d0 \ub300\ud574 \ud558\ub098\uc758 \ucd9c\ub825 \ub3c4\ud050\uba3c\ud2b8 \uc0dd\uc131<\/li><li>$group : \uc9c0\uc815\ub41c \ud0a4\ub85c \ub3c4\ud050\uba3c\ud2b8\ub97c \uadf8\ub8f9\ud654<\/li><li>$sort : \ub3c4\ud050\uba3c\ud2b8 \uc815\ub82c<\/li><li>$geoNear : \uc9c0\ub9ac \uacf5\uac04\uc704\uce58 \uadfc\ucc98\uc758 \ub3c4\ud050\uba3c\ud2b8 \uc120\ud0dd<\/li><li>$out : \ud30c\uc774\ud504\ub77c\uc778\uc758 \uacb0\uacfc\ub97c \uceec\ub809\uc158\uc5d0 \uc4f4\ub2e4<\/li><li>$redact : \ud2b9\uc815 \ub370\uc774\ud130\uc5d0 \ub300\ud55c \uc811\uadfc\uc744 \uc81c\uc5b4<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">db.products.aggregate([{$match: ...}, {$group: ...}, {$sort: ...} ] )<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">-&gt; match, group, sort\ub85c \uad6c\uc131\ub41c \uc9d1\uacc4 \ud504\ub808\uc784\uc6cc\ud06c \ud30c\uc774\ud504\ub77c\uc778<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"823\" height=\"224\" src=\"https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_31_41.png\" alt=\"\" class=\"wp-image-765\" srcset=\"https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_31_41.png 823w, https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_31_41-300x82.png 300w, https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_31_41-768x209.png 768w\" sizes=\"auto, (max-width: 823px) 100vw, 823px\" \/><figcaption>\uc804\uccb4 \uc81c\ud488\uceec\ub809\uc158 -&gt; $match \uc791\uc5c5\uc73c\ub85c \uc804\ub2ec -&gt; $group \uc5f0\uc0b0\uc790\ub85c \uc804\ub2ec (\ud2b9\uc815 \ud0a4\ub85c \uadf8\ub8f9\ud654\ud558\uc5ec \ud569\uacc4 \ubc0f \ud3c9\uade0\uacfc \uac19\uc740 \uc815\ubcf4\uc81c\uacf5) -&gt; $sort \uc5f0\uc0b0\uc790\ub85c \uc804\ub2ec (\uc815\ub82c \uc218\ud589)<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"690\" height=\"346\" src=\"https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_36_26.png\" alt=\"\" class=\"wp-image-766\" srcset=\"https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_36_26.png 690w, https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_36_26-300x150.png 300w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">\uc804\uc790\uc0c1\uac70\ub798 \uc9d1\uacc4 \uc608\uc81c<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"842\" height=\"919\" src=\"https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_40_37.png\" alt=\"\" class=\"wp-image-767\" srcset=\"https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_40_37.png 842w, https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_40_37-275x300.png 275w, https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-13_40_37-768x838.png 768w\" sizes=\"auto, (max-width: 842px) 100vw, 842px\" \/><figcaption>\uc81c\ud488 : \ub9ac\ubdf0 = 1 : n<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\uc0c1\ud488,  \uce74\ud14c\uace0\ub9ac, \ub9ac\ubdf0<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">product = db.products.findOne({'slug':'wheelbarrow-9092'})\nreviews_count = db.reviews.count({'product_id':product['_id']})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\uc81c\ud488\uc758 \ub9ac\ubdf0\uc218 \uacc4\uc0b0 \uc608\uc2dc (5\uc7a5\ub0b4\uc6a9)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">db.reviews.aggregate([\n {$group : { _id:'$product_id', \/\/ product_id\ub85c \uc785\ub825 \ub3c4\ud050\uba3c\ud2b8\ub97c \uadf8\ub8f9\ud654\n count:{$sum:1} }}               \/\/ \uac01 \uc81c\ud488\uc5d0 \ub300\ud55c \ub9ac\ubdf0\uc218\ub97c \uce74\uc6b4\ud2b8\n]);\n\n\/\/ \uac01 \uc81c\ud488\uc5d0 \ub300\ud574 \ud558\ub098\uc758 \ub3c4\ud050\uba3c\ud2b8\ub97c \ucd9c\ub825\n{ \"_id\" : ObjectId(\"4c4b1...82\"), \"count\" : 2 } \n{ \"_id\" : ObjectId(\"4c4b1...81\"), \"count\" : 3 }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/\/\uad00\uc2ec\uc0c1\ud488 \ud55c\uac1c\uc5d0 \ub300\ud55c \ub9ac\ubdf0\uc218 \uce74\uc6b4\ud2b8\nproduct = db.products.findOne({'slug': 'wheelbarrow-9092'})\nratingSummary = db.reviews.aggregate([ \n {$match : { product_id: product['_id']} }, \/\/ \ud558\ub098\uc758 \uc0c1\ud488 \uc120\ud0dd\n {$group : { _id:'$product_id',               \/\/ \uacb0\uacfc\uc758 \uccab \ubc88\uc9f8 \ub3c4\ud050\uba3c\ud2b8\ub97c \ubc18\ud658\n count:{$sum:1} }}\n]).next(); \n\n{ \"_id\" : ObjectId(\"4c4b1...81\"), \"count\" : 3 }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">db.reviews.count({'product_id': product['_id']})<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">$match\ub97c $group \uc55e\uc5d0 \ub450\uc5b4\uc57c \ucc98\ub9ac\ud574\uc57c\ud558\ub294 \ub3c4\ud050\uba3c\ud2b8\ub97c \uc218\ub97c \uc904\uc774\uba74\uc11c \uc5f0\uc0b0\ud560 \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\ud3c9\uade0 \ub9ac\ubdf0 \uacc4\uc0b0\ud558\uae30<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">product = db.products.findOne({'slug': 'wheelbarrow-9092'})\nratingSummary = db.reviews.aggregate([\n {$match : {'product_id': product['_id']}},\n {$group : { _id:'$product_id',\n average:{$avg:'$rating'},                            \/\/ \uc81c\ud488\uc758 \ud3c9\uade0 \ud3c9\uc810\uc744 \uacc4\uc0b0\n count: {$sum:1}}}\n]).next();\n\n{\n \"_id\" : ObjectId(\"4c4b1476238d3b4dd5003981\"),\n \"average\" : 4.333333333333333,\n \"count\" : 3\n} <\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\ub4f1\uae09\ubcc4 \ub9ac\ubdf0 \uacc4\uc0b0\ud558\uae30<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"263\" src=\"https:\/\/scrutineer.co.kr\/wordpress\/wp-content\/uploads\/2021\/10\/screenshot-pepa.holla_.cz-2021.10.28-14_05_05.png\" alt=\"\" class=\"wp-image-772\"\/><figcaption>\uc81c\ud488\uc5d0 \ub300\ud574 5\uba85 \ub9ac\ubdf0\uc5b4\uac00 5\uc810 , 2\uba85\uc774 4\uc810, 1\uba85\uc774 3\uc810<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">countsByRating = db.reviews.aggregate([\n {$match : {'product_id': product['_id']}},    \/\/ \uc81c\ud488\uc120\ud0dd\n {$group : { _id:'$rating',                       \/\/ \ub4f1\uae09\ubcc4\ub85c \uadf8\ub8f9\ud654 ($rating)\n count:{$sum:1}}}                                \/\/ \uac01 \ub4f1\uae09\ubcc4 \ub9ac\ubdf0\uc218 \uacc4\uc0b0\n]).toArray();                                       \/\/ \uacb0\uacfc \ucee4\uc11c\ub97c \ubc30\uc5f4\ub85c \ubcc0\ud658\n\n\n[ { \"_id\" : 5, \"count\" : 5 },\n { \"_id\" : 4, \"count\" : 2 },\n { \"_id\" : 3, \"count\" : 1 } ]\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">SELECT RATING, COUNT(*) AS COUNT\nFROM REVIEWS\nWHERE PRODUCT_ID = '4c4b1476238d3b4dd5003981'\nGROUP BY RATING<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">SQL \ucffc\ub9ac<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\uceec\ub809\uc158 \uc870\uc778<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">db.products.aggregate([\n {$group : { _id:'$main_cat_id',\n count:{$sum:1}}}\n]);\n\n{ \"_id\" : ObjectId(\"6a5b1476238d3b4dd5000048\"), \"count\" : 2 }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">db.mainCategorySummary.remove({}); \/\/ mainCategorySummary \uceec\ub809\uc158\uc5d0\uc11c \uae30\uc874 \ub3c4\ud050\uba3c\ud2b8\ub97c \uc81c\uac70\n\ndb.products.aggregate([\n {$group : { _id:'$main_cat_id',\n count:{$sum:1}}}\n]).forEach(function(doc){\n var category = db.categories.findOne({_id:doc._id});        \/\/\uacb0\uacfc\uc5d0 \ub300\ud55c \uce74\ud14c\uace0\ub9ac \uc77d\uae30\n if (category !== null) {                                             \/\/ \uce74\ud14c\uace0\ub9ac\uac00 \uc5c6\uc744 \ub54c      \n doc.category_name = category.name;\n }\n else {\n doc.category_name = 'not found';\n }\n db.mainCategorySummary.insert(doc);                         \/\/ \uacb0\ud569\ub41c \uacb0\uacfc\ub97c \uc694\uc57d \uceec\ub809\uc158\uc5d0 \uc0bd\uc785\n})\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc9d1\uacc4 MongoDB \uc9d1\uacc4 \ud504\ub808\uc784\uc6cc\ud06c\ub97c \uc0ac\uc6a9\ud558\uc5ec \ubcf4\ub2e4 \ubcf5\uc7a1\ud55c \ucffc\ub9ac \uc0ac\uc6a9\uc744 \uc54c\uc544\ubcf8\ub2e4. (\uc608. \uc6d4\ubcc4\ub9e4\ucd9c, \uc81c\ud488\ubcc4 \ub9e4\ucd9c, \uc0ac\uc6a9\uc790\ubcc4 \uc8fc\ubb38\ud569\uacc4 \ub4f1, cf. GROUP BY) \uc9d1\uacc4 \ud504\ub808\uc784\uc6cc\ud06c \uac1c\uc694 \uc9d1\uacc4 \ud30c\uc774\ud504\ub77c\uc778 $project : \ucd9c\ub825 \ub3c4\ud050\uba3c\ud2b8\uc0c1\uc5d0 \ubc30\uce58\ud560 \ud544\ub4dc \uc9c0\uc815 $match : \ucc98\ub9ac\ub420 \ub3c4\ud050\uba3c\ud2b8\ub97c \uc120\ud0dd\ud558\ub294 \uac83. (= find()) $limit : \ub2e4\uc74c \ub2e8\uacc4\uc5d0 \uc804\ub2ec\ub420 \ub3c4\ud050\uba3c\ud2b8\uc758 \uc218 \uc81c\ud55c $skip : \uc9c0\uc815\ub41c \uc218\uc758 \ub3c4\ud050\uba3c\ud2b8\ub97c \uac74\ub108\ub6f4\ub2e4. $unwind &#8230; <a title=\"\ubabd\uace0\ub514\ube44 \uc778 \uc561\uc158 6\uc7a5 -1\" class=\"read-more\" href=\"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/2021\/10\/28\/%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\/\" aria-label=\"Read more about \ubabd\uace0\ub514\ube44 \uc778 \uc561\uc158 6\uc7a5 -1\">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-762","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\/762","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=762"}],"version-history":[{"count":7,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/762\/revisions"}],"predecessor-version":[{"id":780,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/762\/revisions\/780"}],"wp:attachment":[{"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scrutineer.co.kr\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}