Skip to content

Commit 7f41b20

Browse files
committed
DOCSP-34174: codewhisperer pt 4 (#323)
* DOCSP-34174: codewhisperer pt 4 * highlighting fix * RM PR fixes
1 parent e530cf5 commit 7f41b20

File tree

7 files changed

+28
-11
lines changed

7 files changed

+28
-11
lines changed

source/fundamentals/time-series.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Time Series Collections
55
=======================
66

7-
.. default-domain:: mongodb
8-
97
.. contents:: On this page
108
:local:
119
:backlinks: none
@@ -65,20 +63,19 @@ collection with the ``temperature`` as the time field:
6563
.. literalinclude:: /includes/fundamentals/code-snippets/timeSeries.go
6664
:start-after: begin create ts coll
6765
:end-before: end create ts coll
68-
:emphasize-lines: 2, 5
66+
:emphasize-lines: 4, 7
6967
:language: go
7068
:dedent:
7169

7270
To check if you created the collection, send the ``"listCollections"``
7371
command to the ``RunCommand()`` method:
7472

7573
.. io-code-block::
76-
:caption: Testing whether we created a time series collection.
7774
:copyable: true
7875

7976
.. input:: /includes/fundamentals/code-snippets/timeSeriesRunCommand.go
8077
:language: go
81-
:emphasize-lines: 1, 4
78+
:emphasize-lines: 2, 6
8279

8380
.. output::
8481
:visible: false

source/fundamentals/transactions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ following steps:
128128
.. literalinclude:: /includes/fundamentals/code-snippets/transaction.go
129129
:language: go
130130
:dedent:
131-
:emphasize-lines: 4,8,10-11
131+
:emphasize-lines: 5,10,14-15
132132
:start-after: start-session
133133
:end-before: end-session
134134

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Connects to MongoDB and sets a Stable API version
12
package main
23

34
import (

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Creates a time series collection to record temperature data
12
package main
23

34
import (
@@ -33,21 +34,26 @@ func main() {
3334

3435
// begin create ts coll
3536
db := client.Database("spring_weather")
37+
38+
// Creates a time series collection that stores "temperature" values over time
3639
tso := options.TimeSeries().SetTimeField("temperature")
3740
opts := options.CreateCollection().SetTimeSeriesOptions(tso)
3841

3942
db.CreateCollection(context.TODO(), "march2022", opts)
4043
// end create ts coll
4144

45+
// Runs a command to list information about collections in the
46+
// database
4247
// begin check ts coll
4348
command := bson.D{{"listCollections", 1}}
4449
var result bson.M
45-
50+
4651
commandErr := db.RunCommand(context.TODO(), command).Decode(&result)
4752
if commandErr != nil {
4853
panic(commandErr)
4954
}
5055

56+
// Prints information about the database's collections and views
5157
output, outputErr := json.MarshalIndent(result, "", " ")
5258
if outputErr != nil {
5359
panic(outputErr)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
// Creates a command to list collections
12
command := bson.D{{"listCollections", 1}}
23
var result bson.M
3-
4+
5+
// Runs the command on the database
46
commandErr := db.RunCommand(context.TODO(), command).Decode(&result)
57
if commandErr != nil {
68
panic(commandErr)
79
}
810

11+
// Prints the command results
912
output, outputErr := json.MarshalIndent(result, "", " ")
1013
if outputErr != nil {
1114
panic(outputErr)

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Enable TLS on a connection by using the Go driver
12
package main
23

34
import (
@@ -16,7 +17,7 @@ func main() {
1617
certFile := "<path to public client certificate>"
1718
keyFile := "<path to private client key>"
1819

19-
// Load CA certificate file
20+
// Loads CA certificate file
2021
caCert, err := os.ReadFile(caFile)
2122
if err != nil {
2223
panic(err)
@@ -26,20 +27,24 @@ func main() {
2627
panic("Error: CA file must be in PEM format")
2728
}
2829

29-
// Load client certificate files
30+
// Loads client certificate files
3031
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
3132
if err != nil {
3233
panic(err)
3334
}
3435

35-
// Instantiate a Config
36+
// Instantiates a Config instance
3637
tlsConfig := &tls.Config{
3738
RootCAs: caCertPool,
3839
Certificates: []tls.Certificate{cert},
3940
}
4041

4142
uri := "<connection string>"
43+
44+
// Sets TLS options in options instance
4245
opts := options.Client().ApplyURI(uri).SetTLSConfig(tlsConfig)
46+
47+
// Connects to MongoDB with TLS enabled
4348
client, err := mongo.Connect(context.TODO(), opts)
4449
if err != nil {
4550
panic(err)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Perform a multi-document transaction by using the Go driver
12
package main
23

34
import (
@@ -32,12 +33,16 @@ func main() {
3233
wc := writeconcern.Majority()
3334
txnOptions := options.Transaction().SetWriteConcern(wc)
3435

36+
// Starts a session on the client
3537
session, err := client.StartSession()
3638
if err != nil {
3739
panic(err)
3840
}
41+
// Defers ending the session after the transaction is committed or ended
3942
defer session.EndSession(context.TODO())
4043

44+
// Inserts multiple documents into a collection within a transaction,
45+
// then commits or ends the transaction
4146
result, err := session.WithTransaction(context.TODO(), func(ctx mongo.SessionContext) (interface{}, error) {
4247
result, err := coll.InsertMany(ctx, []interface{}{
4348
bson.D{{"title", "The Bluest Eye"}, {"author", "Toni Morrison"}},

0 commit comments

Comments
 (0)