Created
April 27, 2020 16:07
-
-
Save marantz/7e92a5a446d38235a3c3b18b02383125 to your computer and use it in GitHub Desktop.
MongoDB Aggregation Grouping Basic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // sample | |
| db.test_collection.insert({field01:"A", field02:"a", field03:1}) | |
| db.test_collection.insert({field01:"A", field02:"a", field03:2}) | |
| db.test_collection.insert({field01:"A", field02:"a", field03:3}) | |
| /* | |
| 'view_sum_naver':{ $cond:{ if: {$eq:['naver','$origin']}, then:1, else:0, } }, | |
| 'view_sum_naver':{ $cond:{ if: {$eq:['naver','$origin']}, then:1, else:0, } }, | |
| 'view_sum_naver':{ $cond:{ if: {$eq:['naver','$origin']}, then:1, else:0, } }, | |
| 'view_sum_naver':{ $cond:{ if: {$eq:['naver','$origin']}, then:1, else:0, } }, | |
| 'view_sum_naver':{ $cond:{ if: {$eq:['naver','$origin']}, then:1, else:0, } }, | |
| */ | |
| db.test_collection.aggregate([ | |
| {$sample: {size: 1000}}, | |
| {$project:{ | |
| _id:0, | |
| field01:1, | |
| field02:1, | |
| field03:1 | |
| }}, | |
| {$group:{ | |
| _id:{ | |
| field01:"$field01", | |
| field02:"$field02", | |
| field03:"$field03" | |
| }, | |
| count:{$sum:1} | |
| }}, | |
| {$replaceWith: { $mergeObjects: ["$_id",{count:"$count"}] }}, | |
| {$project:{_id:0}} | |
| ]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment