@@ -5,12 +5,23 @@ import (
55 "fmt"
66 "log"
77 "os"
8+ "strings"
89
910 "go.mongodb.org/mongo-driver/bson"
1011 "go.mongodb.org/mongo-driver/mongo"
1112 "go.mongodb.org/mongo-driver/mongo/options"
1213)
1314
15+ // start-tea-struct
16+ type Tea struct {
17+ Type string
18+ Category string
19+ Toppings []string
20+ Price float32
21+ }
22+
23+ // end-tea-struct
24+
1425func main () {
1526 var uri string
1627 if uri = os .Getenv ("MONGODB_URI" ); uri == "" {
@@ -31,34 +42,32 @@ func main() {
3142 // begin insert docs
3243 coll := client .Database ("tea" ).Collection ("menu" )
3344 docs := []interface {}{
34- bson. D {{ "type" , "Masala" }, { "category" , "black" }, { "toppings" , bson. A {"ginger" , "pumpkin spice" , "cinnomon" }}, { "price" , 6.75 } },
35- bson. D {{ "type" , "Gyokuro" }, { "category" , "green" }, { "toppings" , bson. A {"berries" , "milk foam" }}, { "price" , 5.65 } },
36- bson. D {{ "type" , "English Breakfast" }, { "category" , "black" }, { "toppings" , bson. A {"whipped cream" , "honey" }}, { "price" , 5.75 } },
37- bson. D {{ "type" , "Sencha" }, { "category" , "green" }, { "toppings" , bson. A {"lemon" , "whipped cream" }}, { "price" , 5.15 } },
38- bson. D {{ "type" , "Assam" }, { "category" , "black" }, { "toppings" , bson. A {"milk foam" , "honey" , "berries" }}, { "price" , 5.65 } },
39- bson. D {{ "type" , "Matcha" }, { "category" , "green" }, { "toppings" , bson. A {"whipped cream" , "honey" }}, { "price" , 6.45 } },
40- bson. D {{ "type" , "Earl Grey" }, { "category" , "black" }, { "toppings" , bson. A {"milk foam" , "pumpkin spice" }}, { "price" , 6.15 } },
41- bson. D {{ "type" , "Hojicha" }, { "category" , "green" }, { "toppings" , bson. A {"lemon" , "ginger" , "milk foam" }}, { "price" , 5.55 } },
45+ Tea { Type : "Masala" , Category : "black" , Toppings : [] string {"ginger" , "pumpkin spice" , "cinnamon" }, Price : 6.75 },
46+ Tea { Type : "Gyokuro" , Category : "green" , Toppings : [] string {"berries" , "milk foam" }, Price : 5.65 },
47+ Tea { Type : "English Breakfast" , Category : "black" , Toppings : [] string {"whipped cream" , "honey" }, Price : 5.75 },
48+ Tea { Type : "Sencha" , Category : "green" , Toppings : [] string {"lemon" , "whipped cream" }, Price : 5.15 },
49+ Tea { Type : "Assam" , Category : "black" , Toppings : [] string {"milk foam" , "honey" , "berries" }, Price : 5.65 },
50+ Tea { Type : "Matcha" , Category : "green" , Toppings : [] string {"whipped cream" , "honey" }, Price : 6.45 },
51+ Tea { Type : "Earl Grey" , Category : "black" , Toppings : [] string {"milk foam" , "pumpkin spice" }, Price : 6.15 },
52+ Tea { Type : "Hojicha" , Category : "green" , Toppings : [] string {"lemon" , "ginger" , "milk foam" }, Price : 5.55 },
4253 }
4354
4455 result , err := coll .InsertMany (context .TODO (), docs )
56+ // end insert docs
57+
4558 if err != nil {
4659 panic (err )
4760 }
48- // end insert docs
61+
4962 fmt .Printf ("Number of documents inserted: %d\n " , len (result .InsertedIDs ))
5063
51- fmt .Println ("Average: " )
64+ fmt .Println ("\n Aggregation Example - Average\n " )
5265 {
5366 groupStage := bson.D {
5467 {"$group" , bson.D {
5568 {"_id" , "$category" },
56- {"average_price" , bson.D {
57- {"$avg" , "$price" },
58- }},
59- {"type_total" , bson.D {
60- {"$sum" , 1 },
61- }},
69+ {"average_price" , bson.D {{"$avg" , "$price" }}},
70+ {"type_total" , bson.D {{"$sum" , 1 }}},
6271 }}}
6372
6473 cursor , err := coll .Aggregate (context .TODO (), mongo.Pipeline {groupStage })
@@ -71,32 +80,29 @@ func main() {
7180 panic (err )
7281 }
7382 for _ , result := range results {
74- fmt .Printf ("Average price of %v tea: $%v \n " , result ["_id" ], result ["average_price" ])
75- fmt .Printf ("Amount of %v tea: %v \n \n " , result ["_id" ], result ["type_total" ])
83+ fmt .Printf ("Average price of %v tea options : $%v \n " , result ["_id" ], result ["average_price" ])
84+ fmt .Printf ("Number of %v tea options : %v \n \n " , result ["_id" ], result ["type_total" ])
7685 }
7786 }
7887
79- fmt .Println ("Unset: " )
88+ fmt .Println ("\n Aggregation Example - Unset\n " )
8089 {
8190 matchStage := bson.D {{"$match" , bson.D {{"toppings" , "milk foam" }}}}
8291 unsetStage := bson.D {{"$unset" , bson.A {"_id" , "category" }}}
83- sortStage := bson.D {{"$sort" , bson.D {
84- {"price" , 1 },
85- {"toppings" , 1 }},
86- }}
92+ sortStage := bson.D {{"$sort" , bson.D {{"price" , 1 }, {"toppings" , 1 }}}}
8793 limitStage := bson.D {{"$limit" , 2 }}
8894
8995 cursor , err := coll .Aggregate (context .TODO (), mongo.Pipeline {matchStage , unsetStage , sortStage , limitStage })
9096 if err != nil {
9197 panic (err )
9298 }
9399
94- var results []bson. M
100+ var results []Tea
95101 if err = cursor .All (context .TODO (), & results ); err != nil {
96102 panic (err )
97103 }
98104 for _ , result := range results {
99- fmt .Printf ("Tea: %v \n Toppings: %v \n Price: $%v \n \n " , result [ "type" ], result [ "toppings" ] , result [ "price" ] )
105+ fmt .Printf ("Tea: %v \n Toppings: %v \n Price: $%v \n \n " , result . Type , strings . Join ( result . Toppings , ", " ) , result . Price )
100106 }
101107 }
102108}
0 commit comments