Skip to content

Commit 28af27e

Browse files
committed
DOCSP-34174: codewhisperer pt 6 (#325)
* DOCSP-34174: codewhisperer pt 6 * CD PR fixes
1 parent 785c6e7 commit 28af27e

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

source/fundamentals/aggregation.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Aggregation
55
===========
66

7-
.. default-domain:: mongodb
8-
97
.. contents:: On this page
108
:local:
119
:backlinks: none

source/fundamentals/logging.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ following specifications:
195195
.. literalinclude:: /includes/fundamentals/code-snippets/logging.go
196196
:language: go
197197
:dedent:
198-
:emphasize-lines: 1-4, 9
198+
:emphasize-lines: 1-4, 10
199199
:start-after: start-standard-logger
200200
:end-before: end-standard-logger
201201

@@ -270,7 +270,6 @@ This example demonstrates how to define and implement a custom logger.
270270
.. literalinclude:: /includes/fundamentals/code-snippets/logging.go
271271
:language: go
272272
:dedent:
273-
:emphasize-lines: 1-2, 4-8, 13
274273
:start-after: start-set-customlogger
275274
:end-before: end-set-customlogger
276275

@@ -357,7 +356,6 @@ a third-party logging package, into your application.
357356
.. literalinclude:: /includes/fundamentals/code-snippets/logging.go
358357
:language: go
359358
:dedent:
360-
:emphasize-lines: 1, 3-6, 11
361359
:start-after: start-set-thirdparty-logger
362360
:end-before: end-set-thirdparty-logger
363361

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Updates or inserts a matching document by using the Go driver
12
package main
23

34
import (
@@ -56,11 +57,17 @@ func main() {
5657

5758
fmt.Println("\nUpsert:\n")
5859
{
60+
// Creates a filter to match documents with a specified
61+
// "species" and "plant_id" and an update document to set new fields
5962
// begin upsert
6063
filter := bson.D{{"species", "Ledebouria socialis"}, {"plant_id", 3}}
6164
update := bson.D{{"$set", bson.D{{"species", "Ledebouria socialis"}, {"plant_id", 3}, {"height", 8.3}}}}
65+
66+
// Sets the upsert option to true
6267
opts := options.Update().SetUpsert(true)
6368

69+
// Updates a documents or inserts a document if no documents are
70+
// matched and prints the results
6471
result, err := coll.UpdateOne(context.TODO(), filter, update, opts)
6572
if err != nil {
6673
panic(err)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Performs an aggregation by using the Go driver
12
package main
23

34
import (
@@ -39,6 +40,7 @@ func main() {
3940
}
4041
}()
4142

43+
// Inserts sample documents describing tea varieties
4244
// begin insert docs
4345
coll := client.Database("tea").Collection("menu")
4446
docs := []interface{}{
@@ -63,13 +65,17 @@ func main() {
6365

6466
fmt.Println("\nAggregation Example - Average\n")
6567
{
68+
// Creates a stage to group documents by "category" and
69+
// calculates the average price and total number of documents
70+
// for each "category"
6671
groupStage := bson.D{
6772
{"$group", bson.D{
6873
{"_id", "$category"},
6974
{"average_price", bson.D{{"$avg", "$price"}}},
7075
{"type_total", bson.D{{"$sum", 1}}},
7176
}}}
7277

78+
// Performs the aggregation and prints the results
7379
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{groupStage})
7480
if err != nil {
7581
panic(err)
@@ -87,11 +93,14 @@ func main() {
8793

8894
fmt.Println("\nAggregation Example - Unset\n")
8995
{
96+
// Creates stages to match documents, remove the "category"
97+
// field, specify a sort, and limit the output to 2 documents
9098
matchStage := bson.D{{"$match", bson.D{{"toppings", "milk foam"}}}}
9199
unsetStage := bson.D{{"$unset", bson.A{"_id", "category"}}}
92100
sortStage := bson.D{{"$sort", bson.D{{"price", 1}, {"toppings", 1}}}}
93101
limitStage := bson.D{{"$limit", 2}}
94102

103+
// Performs the aggregation and prints the results
95104
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, unsetStage, sortStage, limitStage})
96105
if err != nil {
97106
panic(err)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Stores and retrieves large files by using the Go driver GridFS feature
12
package main
23

34
import (
@@ -20,6 +21,7 @@ func main() {
2021
}
2122
defer client.Disconnect(context.TODO())
2223

24+
// Creates a GridFS bucket
2325
bucket, err := gridfs.NewBucket(client.Database("foo"))
2426
if err != nil {
2527
panic(err)
@@ -30,7 +32,12 @@ func main() {
3032
if err != nil {
3133
panic(err)
3234
}
35+
36+
// Defines options that specify configuration information for files
37+
// uploaded to the bucket
3338
uploadOpts := options.GridFSUpload().SetChunkSizeBytes(200000)
39+
40+
// Writes a file to an output stream
3441
uploadStream, err := bucket.OpenUploadStream("file.txt", uploadOpts)
3542
if err != nil {
3643
panic(err)

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Configures loggers by using the Go driver
12
package main
23

34
import (
@@ -31,12 +32,14 @@ func main() {
3132
}
3233

3334
func standardLogging(uri string) {
35+
// Sets options when configuring the standard logger
3436
// start-standard-logger
3537
loggerOptions := options.
3638
Logger().
3739
SetMaxDocumentLength(25).
3840
SetComponentLevel(options.LogComponentCommand, options.LogLevelDebug)
3941

42+
// Creates options that include the logger specification
4043
clientOptions := options.
4144
Client().
4245
ApplyURI(uri).
@@ -63,6 +66,7 @@ func standardLogging(uri string) {
6366
}
6467
}
6568

69+
// Creates a struct to model the logger
6670
// start-customlogger-struct
6771
type CustomLogger struct {
6872
io.Writer
@@ -71,6 +75,7 @@ type CustomLogger struct {
7175

7276
// end-customlogger-struct
7377

78+
// Creates functions to specify how the logger generates messages
7479
// start-customlogger-funcs
7580
func (logger *CustomLogger) Info(level int, msg string, _ ...interface{}) {
7681
logger.mu.Lock()
@@ -91,6 +96,7 @@ func (logger *CustomLogger) Error(err error, msg string, _ ...interface{}) {
9196
// end-customlogger-funcs
9297

9398
func customLogging(uri string) {
99+
// Sets options when configuring the custom logger
94100
// start-set-customlogger
95101
buf := bytes.NewBuffer(nil)
96102
sink := &CustomLogger{Writer: buf}
@@ -101,11 +107,12 @@ func customLogging(uri string) {
101107
SetComponentLevel(options.LogComponentCommand, options.LogLevelDebug).
102108
SetComponentLevel(options.LogComponentConnection, options.LogLevelDebug)
103109

110+
// Creates options that include the logger specification
104111
clientOptions := options.
105112
Client().
106113
ApplyURI(uri).
107114
SetLoggerOptions(loggerOptions)
108-
// end-set-customlogger
115+
// end-set-customlogger
109116
client, err := mongo.Connect(context.TODO(), clientOptions)
110117
if err != nil {
111118
panic(err)
@@ -126,6 +133,7 @@ func customLogging(uri string) {
126133
}
127134

128135
func thirdPartyLogging(uri string) {
136+
// Creates a logrus logger
129137
// start-make-logrus
130138
myLogger := &logrus.Logger{
131139
Out: os.Stderr,
@@ -137,19 +145,22 @@ func thirdPartyLogging(uri string) {
137145
}
138146
// end-make-logrus
139147

148+
// Sets the sink for the logger
140149
// start-set-thirdparty-logger
141150
sink := logrusr.New(myLogger).GetSink()
142151

152+
// Sets options when configuring the logrus logger
143153
loggerOptions := options.
144154
Logger().
145155
SetSink(sink).
146156
SetComponentLevel(options.LogComponentCommand, options.LogLevelDebug)
147157

158+
// Creates options that include the logger specification
148159
clientOptions := options.
149160
Client().
150161
ApplyURI(uri).
151162
SetLoggerOptions(loggerOptions)
152-
// end-set-thirdparty-logger
163+
// end-set-thirdparty-logger
153164
client, err := mongo.Connect(context.TODO(), clientOptions)
154165
if err != nil {
155166
panic(err)

0 commit comments

Comments
 (0)