Skip to content

Commit b6131e4

Browse files
GODRIVER-3443 Remove reference to clientOptions builder (#1910)
1 parent c7de6c1 commit b6131e4

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

docs/migration-2.0.md

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -532,13 +532,13 @@ function MergeOptions(target, optionsList):
532532
return target
533533
```
534534

535-
Currently, the driver maintains this logic for every options type, e.g. [MergeClientOptions](https://github.com/mongodb/mongo-go-driver/blob/2e7cb372b05cba29facd58aac7e715c3cec4e377/mongo/options/clientoptions.go#L1065). For v2, we’ve decided to abstract the merge functions by changing the options builder pattern to maintain a slice of setter functions, rather than setting data directly to an options object. Typical usage of options should not change, for example the following is still honored:
535+
Currently, the driver maintains this logic for every options type, e.g. [MergeFindOptions](https://github.com/mongodb/mongo-go-driver/blob/2e7cb372b05cba29facd58aac7e715c3cec4e377/mongo/options/findoptions.go#L257). For v2, we’ve decided to abstract the merge functions by changing the options builder pattern to maintain a slice of setter functions, rather than setting data directly to an options object. Typical usage of options should not change, for example the following is still honored:
536536

537537
```go
538-
opts1 := options.Client().SetAppName("appName")
539-
opts2 := options.Client().SetConnectTimeout(math.MaxInt64)
538+
opts1 := options.Find().SetBatchSize(1)
539+
opts2 := options.Find().SetComment("foo")
540540

541-
_, err := mongo.Connect(opts1, opts2)
541+
_, err := coll.Find(context.TODO(), bson.D{{"x", 1"}}, opts1, opts2)
542542
if err != nil {
543543
panic(err)
544544
}
@@ -553,29 +553,27 @@ The options builder is now a slice of setters, rather than a single options obje
553553
```go
554554
// v1
555555
556-
opts := options.Client().ApplyURI("mongodb://x:y@localhost:27017")
556+
opts := options.Find().SetBatchSize(1)
557557
558-
if opts.Auth.Username == "x" {
559-
opts.Auth.Password = "z"
558+
if opts.MaxAwaitTime == nil {
559+
opts.MaxAwaitTime = &defaultMaxAwaitTime
560560
}
561561
```
562562
563563
```go
564-
565564
// v2
566565
567-
opts := options.Client().ApplyURI("mongodb://x:y@localhost:27017")
566+
opts := options.Find().SetBatchSize(1)
568567
569-
// If the username is "x", use password "z"
570-
pwSetter := func(opts *options.ClientOptions) error {
571-
if opts.Auth.Username == "x" {
572-
opts.Auth.Password = "z"
568+
maxAwaitTimeSetter := func(opts *options.FindOptions) error {
569+
if opts.MaxAwaitTime == nil {
570+
opts.MaxAwaitTime = &defaultMaxAwaitTime
573571
}
574572
575573
return nil
576574
}
577575
578-
opts.Opts = append(opts.Opts, pwSetter)
576+
opts.Opts = append(opts.Opts, maxAwaitTimeSetter)
579577
```
580578
581579
#### Creating a Slice of Options
@@ -585,22 +583,21 @@ Using options created with the builder pattern as elements in a slice:
585583
```go
586584
// v1
587585
588-
opts1 := options.Client().SetAppName("appName")
589-
opts2 := options.Client().SetConnectTimeout(math.MaxInt64)
586+
opts1 := options.Find().SetBatchSize(1)
587+
opts2 := options.Find().SetComment("foo")
590588
591-
opts := []*options.ClientOptions{opts1, opts2}
592-
_, err := mongo.Connect(opts...)
589+
opts := []*options.FindOptions{opts1, opts2}
590+
_, err := coll.Find(context.TODO(), bson.D{{"x", 1"}}, opts...)
593591
```
594592

595593
```go
596594
// v2
597595

598-
opts1 := options.Client().SetAppName("appName")
599-
opts2 := options.Client().SetConnectTimeout(math.MaxInt64)
596+
opts1 := options.Find().SetBatchSize(1)
597+
opts2 := options.Find().SetComment("foo")
600598

601-
// Option objects are "Listers" in v2, objects that hold a list of setters
602-
opts := []options.Lister[options.ClientOptions]{opts1, opts2}
603-
_, err := mongo.Connect(opts...)
599+
opts := []options.Lister[options.FindOptions]{opts1, opts2}
600+
_, err := coll.Find(context.TODO(), bson.D{{"x", 1"}}, opts...)
604601
```
605602
606603
#### Creating Options from Builder
@@ -610,21 +607,21 @@ Since a builder is just a slice of option setters, users can create options dire
610607
```go
611608
// v1
612609
613-
opt := &options.ClientOptions{}
614-
opt.ApplyURI(uri)
610+
opt := &options.FindOptions{}
611+
opt.SetBatchSize(1)
615612
616-
return clientOptionAdder{option: opt}
613+
return findOptionAdder{option: opt}
617614
```
618615
619616
```go
620617
// v2
621618
622-
var opts options.ClientOptions
623-
for _, set := range options.Client().ApplyURI(uri).Opts {
619+
var opts options.FindOptions
620+
for _, set := range options.Find().SetBatchSize(1).Opts {
624621
_ = set(&opts)
625622
}
626623
627-
return clientOptionAdder{option: &opts}
624+
return findOptionAdder{option: &opts}
628625
```
629626
630627
### DeleteManyOptions / DeleteOneOptions
@@ -719,7 +716,7 @@ The `WTimeout` field has been removed from the `WriteConcern` struct. Instead, u
719716
720717
## Bsoncodecs / Bsonoptions Package
721718
722-
`*Codec` structs and `New*Codec` methods have been removed. Additionally, the correlated `bson/bsonoptions` package has been removed, so codecs are not directly configurable using `*CodecOptions` structs in Go Driver 2.0. To configure the encode and decode behavior, use the configuration methods on a `bson.Encoder` or `bson.Decoder`. To configure the encode and decode behavior for a `mongo.Client`, use `options.ClientOptionsBuilder.SetBSONOptions` with `BSONOptions`.
719+
`*Codec` structs and `New*Codec` methods have been removed. Additionally, the correlated `bson/bsonoptions` package has been removed, so codecs are not directly configurable using `*CodecOptions` structs in Go Driver 2.0. To configure the encode and decode behavior, use the configuration methods on a `bson.Encoder` or `bson.Decoder`. To configure the encode and decode behavior for a `mongo.Client`, use `options.ClientOptions.SetBSONOptions` with `BSONOptions`.
723720
724721
This example shows how to set `ObjectIDAsHex`.
725722

0 commit comments

Comments
 (0)