Skip to content

Commit

Permalink
GODRIVER-2004 Add Versioned API connection examples for Docs (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjirewis authored May 14, 2021
1 parent 8aa4b5b commit 29547bc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
97 changes: 97 additions & 0 deletions examples/documentation_examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -2712,3 +2712,100 @@ func IndexExamples(t *testing.T, db *mongo.Database) {
require.NoError(t, err)
}
}

// Start Versioned API Example 1

// VersionedAPIExample is an example of creating a client with versioned API.
func VersionedAPIExample() {
ctx := context.Background()
// For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
// uri := "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl"
// For a sharded cluster, connect to the mongos instances; e.g.
// uri := "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/"
uri := "mongodb://localhost:27017"

serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
panic(err)
}
defer func() { _ = client.Disconnect(ctx) }()
}

// End Versioned API Example 1

// Start Versioned API Example 2

// VersionedAPIStrictExample is an example of creating a client with strict versioned API.
func VersionedAPIStrictExample() {
ctx := context.Background()
// For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
// uri := "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl"
// For a sharded cluster, connect to the mongos instances; e.g.
// uri := "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/"
uri := "mongodb://localhost:27017"

serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetStrict(true)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
panic(err)
}
defer func() { _ = client.Disconnect(ctx) }()
}

// End Versioned API Example 2

// Start Versioned API Example 3

// VersionedAPINonStrictExample is an example of creating a client with non-strict versioned API.
func VersionedAPINonStrictExample() {
ctx := context.Background()
// For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
// uri := "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl"
// For a sharded cluster, connect to the mongos instances; e.g.
// uri := "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/"
uri := "mongodb://localhost:27017"

serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetStrict(false)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
panic(err)
}
defer func() { _ = client.Disconnect(ctx) }()
}

// End Versioned API Example 3

// Start Versioned API Example 4

// VersionedAPIDeprecationErrorsExample is an example of creating a client with versioned API
// with deprecation errors.
func VersionedAPIDeprecationErrorsExample() {
ctx := context.Background()
// For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
// uri := "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl"
// For a sharded cluster, connect to the mongos instances; e.g.
// uri := "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/"
uri := "mongodb://localhost:27017"

serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetDeprecationErrors(true)
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
panic(err)
}
defer func() { _ = client.Disconnect(ctx) }()
}

// End Versioned API Example 4

// VersionedAPIExamples runs all versioned API examples.
func VersionedAPIExamples() {
VersionedAPIExample()
VersionedAPIStrictExample()
VersionedAPINonStrictExample()
VersionedAPIDeprecationErrorsExample()
}
1 change: 1 addition & 0 deletions examples/documentation_examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestDocumentationExamples(t *testing.T) {
documentation_examples.DeleteExamples(t, db)
documentation_examples.RunCommandExamples(t, db)
documentation_examples.IndexExamples(t, db)
documentation_examples.VersionedAPIExamples()
}

func TestAggregationExamples(t *testing.T) {
Expand Down

0 comments on commit 29547bc

Please sign in to comment.