Skip to content

Commit 45416d9

Browse files
authored
Code file fixes (#352)
* most code fixes * more edits * context
1 parent ef4520f commit 45416d9

File tree

8 files changed

+73
-67
lines changed

8 files changed

+73
-67
lines changed

source/fundamentals/aggregation.txt

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,11 @@ the ``$sum`` expression operator.
118118
.. io-code-block::
119119
:copyable: true
120120

121-
.. input::
121+
.. input:: /includes/fundamentals/code-snippets/aggregation.go
122+
:start-after: begin average
123+
:end-before: end average
122124
:language: go
123-
124-
// create group stage
125-
groupStage := bson.D{
126-
{"$group", bson.D{
127-
{"_id", "$category"},
128-
{"average_price", bson.D{{"$avg", "$price"}}},
129-
{"type_total", bson.D{{"$sum", 1}}},
130-
}}}
131-
132-
// pass the pipeline to the Aggregate() method
133-
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{groupStage})
134-
if err != nil {
135-
panic(err)
136-
}
137-
138-
// display the results
139-
var results []bson.M
140-
if err = cursor.All(context.TODO(), &results); err != nil {
141-
panic(err)
142-
}
143-
for _, result := range results {
144-
fmt.Printf("Average price of %v tea options: $%v \n", result["_id"], result["average_price"])
145-
fmt.Printf("Number of %v tea options: %v \n\n", result["_id"], result["type_total"])
146-
}
125+
:dedent:
147126

148127
.. output::
149128
:language: none
@@ -171,29 +150,11 @@ The aggregation pipeline contains the following stages:
171150
.. io-code-block::
172151
:copyable: true
173152

174-
.. input::
153+
.. input:: /includes/fundamentals/code-snippets/aggregation.go
154+
:start-after: begin unset
155+
:end-before: end unset
175156
:language: go
176-
177-
// create the stages
178-
matchStage := bson.D{{"$match", bson.D{{"toppings", "milk foam"}}}}
179-
unsetStage := bson.D{{"$unset", bson.A{"_id", "category"}}}
180-
sortStage := bson.D{{"$sort", bson.D{{"price", 1}, {"toppings", 1}}}}
181-
limitStage := bson.D{{"$limit", 2}}
182-
183-
// pass the pipeline to the Aggregate() method
184-
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, unsetStage, sortStage, limitStage})
185-
if err != nil {
186-
panic(err)
187-
}
188-
189-
// display the results
190-
var results []Tea
191-
if err = cursor.All(context.TODO(), &results); err != nil {
192-
panic(err)
193-
}
194-
for _, result := range results {
195-
fmt.Printf("Tea: %v \nToppings: %v \nPrice: $%v \n\n", result.Type, strings.Join(result.Toppings, ", "), result.Price)
196-
}
157+
:dedent:
197158

198159
.. output::
199160
:language: none

source/fundamentals/crud/write-operations/embedded-arrays.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ This example performs the following actions:
9696
.. input::
9797
:language: go
9898

99-
10099
filter := bson.D{{"sizes", bson.D{{"$lte", 16}}}}
101100
update := bson.D{{"$inc", bson.D{{"sizes.$", -2}}}}
102101
opts := options.FindOneAndUpdate().

source/includes/fundamentals/code-snippets/CRUD/distinctValues.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Course struct {
2323

2424
func main() {
2525
var uri string
26-
if uri = os.Getenv("DRIVER_REF_URI"); uri == "" {
26+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
2727
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/")
2828
}
2929

source/includes/fundamentals/code-snippets/CRUD/insertOptions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ type Book struct {
1515

1616
// end-book-struct
1717

18+
func main() {
19+
insertManyOpts()
20+
insertOneOpts()
21+
}
22+
1823
func insertManyOpts() {
1924
// Sets options to bypass document validation and specify an
2025
// unordered insert when inserting multiple documents

source/includes/fundamentals/code-snippets/aggregation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func main() {
6868
// Creates a stage to group documents by "category" and
6969
// calculates the average price and total number of documents
7070
// for each "category"
71+
// begin average
7172
groupStage := bson.D{
7273
{"$group", bson.D{
7374
{"_id", "$category"},
@@ -89,12 +90,14 @@ func main() {
8990
fmt.Printf("Average price of %v tea options: $%v \n", result["_id"], result["average_price"])
9091
fmt.Printf("Number of %v tea options: %v \n\n", result["_id"], result["type_total"])
9192
}
93+
// end average
9294
}
9395

9496
fmt.Println("\nAggregation Example - Unset\n")
9597
{
9698
// Creates stages to match documents, remove the "category"
9799
// field, specify a sort, and limit the output to 2 documents
100+
// begin unset
98101
matchStage := bson.D{{"$match", bson.D{{"toppings", "milk foam"}}}}
99102
unsetStage := bson.D{{"$unset", bson.A{"_id", "category"}}}
100103
sortStage := bson.D{{"$sort", bson.D{{"price", 1}, {"toppings", 1}}}}
@@ -113,5 +116,6 @@ func main() {
113116
for _, result := range results {
114117
fmt.Printf("Tea: %v \nToppings: %v \nPrice: $%v \n\n", result.Type, strings.Join(result.Toppings, ", "), result.Price)
115118
}
119+
// end unset
116120
}
117121
}

source/includes/fundamentals/code-snippets/gridfs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99

1010
"go.mongodb.org/mongo-driver/mongo"
11+
"go.mongodb.org/mongo-driver/mongo/gridfs"
1112
"go.mongodb.org/mongo-driver/mongo/options"
1213
)
1314

@@ -22,7 +23,10 @@ func main() {
2223

2324
// Creates a GridFS bucket
2425
db := client.Database("myDB")
25-
bucket := db.GridFSBucket()
26+
bucket, err := gridfs.NewBucket(db)
27+
if err != nil {
28+
panic(err)
29+
}
2630

2731
// begin OpenUploadStream example
2832
file, err := os.Open("home/documents/file.txt")

source/includes/fundamentals/code-snippets/srv.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ package main
44
import (
55
"context"
66
"fmt"
7+
"log"
8+
"os"
79

810
"go.mongodb.org/mongo-driver/bson"
911
"go.mongodb.org/mongo-driver/mongo"
1012
"go.mongodb.org/mongo-driver/mongo/options"
1113
)
1214

13-
// Replace the placeholder with your Atlas connection string
14-
const uri = "<connection string>"
15-
1615
func main() {
16+
var uri string
17+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
18+
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
19+
}
1720

1821
// Use the SetServerAPIOptions() method to set the Stable API version to 1
1922
serverAPI := options.ServerAPI(options.ServerAPIVersion1)

source/includes/fundamentals/code-snippets/timeSeriesRunCommand.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
1-
// Creates a command to list collections
2-
command := bson.D{{"listCollections", 1}}
3-
var result bson.M
4-
5-
// Runs the command on the database
6-
commandErr := db.RunCommand(context.TODO(), command).Decode(&result)
7-
if commandErr != nil {
8-
panic(commandErr)
9-
}
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"go.mongodb.org/mongo-driver/bson"
11+
"go.mongodb.org/mongo-driver/mongo"
12+
"go.mongodb.org/mongo-driver/mongo/options"
13+
)
14+
15+
func main() {
16+
var uri string
17+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
18+
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/")
19+
}
20+
21+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
22+
23+
if err != nil {
24+
panic(err)
25+
}
26+
defer client.Disconnect(context.TODO())
27+
28+
db := client.Database("myDB")
29+
30+
// Creates a command to list collections
31+
command := bson.D{{"listCollections", 1}}
32+
var result bson.M
33+
34+
// Runs the command on the database
35+
commandErr := db.RunCommand(context.TODO(), command).Decode(&result)
36+
if commandErr != nil {
37+
panic(commandErr)
38+
}
1039

11-
// Prints the command results
12-
output, outputErr := json.MarshalIndent(result, "", " ")
13-
if outputErr != nil {
14-
panic(outputErr)
40+
// Prints the command results
41+
output, outputErr := json.MarshalIndent(result, "", " ")
42+
if outputErr != nil {
43+
panic(outputErr)
44+
}
45+
fmt.Printf("%s\n", output)
1546
}
16-
fmt.Printf("%s\n", output)

0 commit comments

Comments
 (0)