Skip to content

Commit 29547bc

Browse files
authored
GODRIVER-2004 Add Versioned API connection examples for Docs (#665)
1 parent 8aa4b5b commit 29547bc

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

examples/documentation_examples/examples.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,3 +2712,100 @@ func IndexExamples(t *testing.T, db *mongo.Database) {
27122712
require.NoError(t, err)
27132713
}
27142714
}
2715+
2716+
// Start Versioned API Example 1
2717+
2718+
// VersionedAPIExample is an example of creating a client with versioned API.
2719+
func VersionedAPIExample() {
2720+
ctx := context.Background()
2721+
// For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
2722+
// uri := "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl"
2723+
// For a sharded cluster, connect to the mongos instances; e.g.
2724+
// uri := "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/"
2725+
uri := "mongodb://localhost:27017"
2726+
2727+
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
2728+
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
2729+
client, err := mongo.Connect(ctx, clientOpts)
2730+
if err != nil {
2731+
panic(err)
2732+
}
2733+
defer func() { _ = client.Disconnect(ctx) }()
2734+
}
2735+
2736+
// End Versioned API Example 1
2737+
2738+
// Start Versioned API Example 2
2739+
2740+
// VersionedAPIStrictExample is an example of creating a client with strict versioned API.
2741+
func VersionedAPIStrictExample() {
2742+
ctx := context.Background()
2743+
// For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
2744+
// uri := "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl"
2745+
// For a sharded cluster, connect to the mongos instances; e.g.
2746+
// uri := "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/"
2747+
uri := "mongodb://localhost:27017"
2748+
2749+
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetStrict(true)
2750+
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
2751+
client, err := mongo.Connect(ctx, clientOpts)
2752+
if err != nil {
2753+
panic(err)
2754+
}
2755+
defer func() { _ = client.Disconnect(ctx) }()
2756+
}
2757+
2758+
// End Versioned API Example 2
2759+
2760+
// Start Versioned API Example 3
2761+
2762+
// VersionedAPINonStrictExample is an example of creating a client with non-strict versioned API.
2763+
func VersionedAPINonStrictExample() {
2764+
ctx := context.Background()
2765+
// For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
2766+
// uri := "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl"
2767+
// For a sharded cluster, connect to the mongos instances; e.g.
2768+
// uri := "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/"
2769+
uri := "mongodb://localhost:27017"
2770+
2771+
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetStrict(false)
2772+
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
2773+
client, err := mongo.Connect(ctx, clientOpts)
2774+
if err != nil {
2775+
panic(err)
2776+
}
2777+
defer func() { _ = client.Disconnect(ctx) }()
2778+
}
2779+
2780+
// End Versioned API Example 3
2781+
2782+
// Start Versioned API Example 4
2783+
2784+
// VersionedAPIDeprecationErrorsExample is an example of creating a client with versioned API
2785+
// with deprecation errors.
2786+
func VersionedAPIDeprecationErrorsExample() {
2787+
ctx := context.Background()
2788+
// For a replica set, include the replica set name and a seedlist of the members in the URI string; e.g.
2789+
// uri := "mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017/?replicaSet=myRepl"
2790+
// For a sharded cluster, connect to the mongos instances; e.g.
2791+
// uri := "mongodb://mongos0.example.com:27017,mongos1.example.com:27017/"
2792+
uri := "mongodb://localhost:27017"
2793+
2794+
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1).SetDeprecationErrors(true)
2795+
clientOpts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPIOptions)
2796+
client, err := mongo.Connect(ctx, clientOpts)
2797+
if err != nil {
2798+
panic(err)
2799+
}
2800+
defer func() { _ = client.Disconnect(ctx) }()
2801+
}
2802+
2803+
// End Versioned API Example 4
2804+
2805+
// VersionedAPIExamples runs all versioned API examples.
2806+
func VersionedAPIExamples() {
2807+
VersionedAPIExample()
2808+
VersionedAPIStrictExample()
2809+
VersionedAPINonStrictExample()
2810+
VersionedAPIDeprecationErrorsExample()
2811+
}

examples/documentation_examples/examples_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func TestDocumentationExamples(t *testing.T) {
4747
documentation_examples.DeleteExamples(t, db)
4848
documentation_examples.RunCommandExamples(t, db)
4949
documentation_examples.IndexExamples(t, db)
50+
documentation_examples.VersionedAPIExamples()
5051
}
5152

5253
func TestAggregationExamples(t *testing.T) {

0 commit comments

Comments
 (0)