Skip to content

Commit

Permalink
DOCSP-34174: codewhisperer pt 4 (#323)
Browse files Browse the repository at this point in the history
* DOCSP-34174: codewhisperer pt 4

* highlighting fix

* RM PR fixes
  • Loading branch information
rustagir authored Nov 29, 2023
1 parent 12d4efd commit c1e75aa
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
7 changes: 2 additions & 5 deletions source/fundamentals/time-series.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
Time Series Collections
=======================

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
Expand Down Expand Up @@ -65,20 +63,19 @@ collection with the ``temperature`` as the time field:
.. literalinclude:: /includes/fundamentals/code-snippets/timeSeries.go
:start-after: begin create ts coll
:end-before: end create ts coll
:emphasize-lines: 2, 5
:emphasize-lines: 4, 7
:language: go
:dedent:

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

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

.. input:: /includes/fundamentals/code-snippets/timeSeriesRunCommand.go
:language: go
:emphasize-lines: 1, 4
:emphasize-lines: 2, 6

.. output::
:visible: false
Expand Down
2 changes: 1 addition & 1 deletion source/fundamentals/transactions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ following steps:
.. literalinclude:: /includes/fundamentals/code-snippets/transaction.go
:language: go
:dedent:
:emphasize-lines: 4,8,10-11
:emphasize-lines: 5,10,14-15
:start-after: start-session
:end-before: end-session

Expand Down
1 change: 1 addition & 0 deletions source/includes/fundamentals/code-snippets/srv.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Connects to MongoDB and sets a Stable API version
package main

import (
Expand Down
8 changes: 7 additions & 1 deletion source/includes/fundamentals/code-snippets/timeSeries.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Creates a time series collection to record temperature data
package main

import (
Expand Down Expand Up @@ -33,21 +34,26 @@ func main() {

// begin create ts coll
db := client.Database("spring_weather")

// Creates a time series collection that stores "temperature" values over time
tso := options.TimeSeries().SetTimeField("temperature")
opts := options.CreateCollection().SetTimeSeriesOptions(tso)

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

// Runs a command to list information about collections in the
// database
// begin check ts coll
command := bson.D{{"listCollections", 1}}
var result bson.M

commandErr := db.RunCommand(context.TODO(), command).Decode(&result)
if commandErr != nil {
panic(commandErr)
}

// Prints information about the database's collections and views
output, outputErr := json.MarshalIndent(result, "", " ")
if outputErr != nil {
panic(outputErr)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Creates a command to list collections
command := bson.D{{"listCollections", 1}}
var result bson.M


// Runs the command on the database
commandErr := db.RunCommand(context.TODO(), command).Decode(&result)
if commandErr != nil {
panic(commandErr)
}

// Prints the command results
output, outputErr := json.MarshalIndent(result, "", " ")
if outputErr != nil {
panic(outputErr)
Expand Down
11 changes: 8 additions & 3 deletions source/includes/fundamentals/code-snippets/tls.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Enable TLS on a connection by using the Go driver
package main

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

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

// Load client certificate files
// Loads client certificate files
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
panic(err)
}

// Instantiate a Config
// Instantiates a Config instance
tlsConfig := &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{cert},
}

uri := "<connection string>"

// Sets TLS options in options instance
opts := options.Client().ApplyURI(uri).SetTLSConfig(tlsConfig)

// Connects to MongoDB with TLS enabled
client, err := mongo.Connect(context.TODO(), opts)
if err != nil {
panic(err)
Expand Down
5 changes: 5 additions & 0 deletions source/includes/fundamentals/code-snippets/transaction.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Perform a multi-document transaction by using the Go driver
package main

import (
Expand Down Expand Up @@ -32,12 +33,16 @@ func main() {
wc := writeconcern.Majority()
txnOptions := options.Transaction().SetWriteConcern(wc)

// Starts a session on the client
session, err := client.StartSession()
if err != nil {
panic(err)
}
// Defers ending the session after the transaction is committed or ended
defer session.EndSession(context.TODO())

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

0 comments on commit c1e75aa

Please sign in to comment.