...
1
2
3
4
5
6
7 package readpref_test
8
9 import (
10 "context"
11
12 "go.mongodb.org/mongo-driver/mongo"
13 "go.mongodb.org/mongo-driver/mongo/options"
14 "go.mongodb.org/mongo-driver/mongo/readpref"
15 "go.mongodb.org/mongo-driver/tag"
16 )
17
18
19
20 func ExampleWithTags() {
21 rp := readpref.Nearest(
22 readpref.WithTags(
23 "region", "South",
24 "datacenter", "A"))
25
26 opts := options.Client().
27 ApplyURI("mongodb://localhost:27017").
28 SetReadPreference(rp)
29
30 _, err := mongo.Connect(context.Background(), opts)
31 if err != nil {
32 panic(err)
33 }
34 }
35
36
37
38
39
40
41
42
43
44
45 func ExampleWithTagSets() {
46 tagSetList := tag.NewTagSetsFromMaps([]map[string]string{
47 {"region": "South", "datacenter": "A"},
48 {"region": "South"},
49 {},
50 })
51
52 rp := readpref.Nearest(readpref.WithTagSets(tagSetList...))
53
54 opts := options.Client().
55 ApplyURI("mongodb://localhost").
56 SetReadPreference(rp)
57
58 _, err := mongo.Connect(context.Background(), opts)
59 if err != nil {
60 panic(err)
61 }
62 }
63
View as plain text