Skip to content

Commit cbef64e

Browse files
committed
Add mongo/updateopt
GODRIVER-272 Change-Id: Ia663dfa242af5073aa774d1d25b7fb3c2f1ad3ff
1 parent 1e1fd10 commit cbef64e

File tree

5 files changed

+511
-24
lines changed

5 files changed

+511
-24
lines changed

mongo/collection.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/mongodb/mongo-go-driver/core/writeconcern"
2222
"github.com/mongodb/mongo-go-driver/mongo/aggregateopt"
2323
"github.com/mongodb/mongo-go-driver/mongo/findopt"
24+
"github.com/mongodb/mongo-go-driver/mongo/updateopt"
2425
)
2526

2627
// Collection performs operations on a given collection.
@@ -291,7 +292,7 @@ func (coll *Collection) updateOrReplaceOne(ctx context.Context, filter,
291292
// into a *bson.Document. See TransformDocument for the list of valid types for
292293
// filter and update.
293294
func (coll *Collection) UpdateOne(ctx context.Context, filter interface{}, update interface{},
294-
options ...option.UpdateOptioner) (*UpdateResult, error) {
295+
options ...updateopt.Update) (*UpdateResult, error) {
295296

296297
if ctx == nil {
297298
ctx = context.Background()
@@ -311,7 +312,12 @@ func (coll *Collection) UpdateOne(ctx context.Context, filter interface{}, updat
311312
return nil, err
312313
}
313314

314-
return coll.updateOrReplaceOne(ctx, f, u, options...)
315+
updOpts, err := updateopt.BundleUpdate(options...).Unbundle(true)
316+
if err != nil {
317+
return nil, err
318+
}
319+
320+
return coll.updateOrReplaceOne(ctx, f, u, updOpts...)
315321
}
316322

317323
// UpdateMany updates multiple documents in the collection. A user can supply
@@ -321,7 +327,7 @@ func (coll *Collection) UpdateOne(ctx context.Context, filter interface{}, updat
321327
// into a *bson.Document. See TransformDocument for the list of valid types for
322328
// filter and update.
323329
func (coll *Collection) UpdateMany(ctx context.Context, filter interface{}, update interface{},
324-
opts ...option.UpdateOptioner) (*UpdateResult, error) {
330+
opts ...updateopt.Update) (*UpdateResult, error) {
325331

326332
if ctx == nil {
327333
ctx = context.Background()
@@ -349,11 +355,16 @@ func (coll *Collection) UpdateMany(ctx context.Context, filter interface{}, upda
349355
),
350356
}
351357

358+
updOpts, err := updateopt.BundleUpdate(opts...).Unbundle(true)
359+
if err != nil {
360+
return nil, err
361+
}
362+
352363
oldns := coll.namespace()
353364
cmd := command.Update{
354365
NS: command.Namespace{DB: oldns.DB, Collection: oldns.Collection},
355366
Docs: updateDocs,
356-
Opts: opts,
367+
Opts: updOpts,
357368
}
358369

359370
r, err := dispatch.Update(ctx, cmd, coll.client.topology, coll.writeSelector, coll.writeConcern)

mongo/collection_internal_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/mongodb/mongo-go-driver/internal/testutil"
2020
"github.com/mongodb/mongo-go-driver/mongo/aggregateopt"
2121
"github.com/mongodb/mongo-go-driver/mongo/findopt"
22+
"github.com/mongodb/mongo-go-driver/mongo/updateopt"
2223
"github.com/stretchr/testify/assert"
2324
"github.com/stretchr/testify/require"
2425
)
@@ -638,7 +639,7 @@ func TestCollection_UpdateOne_upsert(t *testing.T) {
638639
update := bson.NewDocument(
639640
bson.EC.SubDocumentFromElements("$inc", bson.EC.Int32("x", 1)))
640641

641-
result, err := coll.UpdateOne(context.Background(), filter, update, Opt.Upsert(true))
642+
result, err := coll.UpdateOne(context.Background(), filter, update, updateopt.Upsert(true))
642643
require.Nil(t, err)
643644
require.Equal(t, result.MatchedCount, int64(0))
644645
require.Equal(t, result.ModifiedCount, int64(0))
@@ -698,9 +699,8 @@ func TestCollection_UpdateOne_WriteConcernError(t *testing.T) {
698699
)
699700
coll := createTestCollection(t, nil, nil)
700701

701-
optwc, err := Opt.WriteConcern(writeconcern.New(writeconcern.W(25)))
702-
require.NoError(t, err)
703-
_, err = coll.UpdateOne(context.Background(), filter, update, optwc)
702+
optwc := updateopt.WriteConcern(writeconcern.New(writeconcern.W(25)))
703+
_, err := coll.UpdateOne(context.Background(), filter, update, optwc)
704704
got, ok := err.(WriteConcernError)
705705
if !ok {
706706
t.Errorf("Did not receive correct type of error. got %T; want %T", err, WriteConcernError{})
@@ -775,7 +775,7 @@ func TestCollection_UpdateMany_upsert(t *testing.T) {
775775
update := bson.NewDocument(
776776
bson.EC.SubDocumentFromElements("$inc", bson.EC.Int32("x", 1)))
777777

778-
result, err := coll.UpdateMany(context.Background(), filter, update, Opt.Upsert(true))
778+
result, err := coll.UpdateMany(context.Background(), filter, update, updateopt.Upsert(true))
779779
require.Nil(t, err)
780780
require.Equal(t, result.MatchedCount, int64(0))
781781
require.Equal(t, result.ModifiedCount, int64(0))
@@ -836,9 +836,8 @@ func TestCollection_UpdateMany_WriteConcernError(t *testing.T) {
836836
)
837837
coll := createTestCollection(t, nil, nil)
838838

839-
optwc, err := Opt.WriteConcern(writeconcern.New(writeconcern.W(25)))
840-
require.NoError(t, err)
841-
_, err = coll.UpdateMany(context.Background(), filter, update, optwc)
839+
optwc := updateopt.WriteConcern(writeconcern.New(writeconcern.W(25)))
840+
_, err := coll.UpdateMany(context.Background(), filter, update, optwc)
842841
got, ok := err.(WriteConcernError)
843842
if !ok {
844843
t.Errorf("Did not receive correct type of error. got %T; want %T", err, WriteConcernError{})

mongo/crud_spec_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/mongodb/mongo-go-driver/internal/testutil/helpers"
1717
"github.com/mongodb/mongo-go-driver/mongo/aggregateopt"
1818
"github.com/mongodb/mongo-go-driver/mongo/findopt"
19+
"github.com/mongodb/mongo-go-driver/mongo/updateopt"
1920
"github.com/stretchr/testify/require"
2021
)
2122

@@ -705,20 +706,19 @@ func updateManyTest(t *testing.T, coll *Collection, test *testCase) {
705706
replaceFloatsWithInts(filter)
706707
replaceFloatsWithInts(update)
707708

708-
var opts []option.UpdateOptioner
709+
var opts []updateopt.Update
709710

710711
if arrayFilters, found := test.Operation.Arguments["arrayFilters"]; found {
711-
arrayFiltersOpt, err := Opt.ArrayFilters(arrayFilters.([]interface{})...)
712-
require.NoError(t, err)
713-
opts = append(opts, arrayFiltersOpt)
712+
arrayFiltersSlice := arrayFilters.([]interface{})
713+
opts = append(opts, updateopt.ArrayFilters(arrayFiltersSlice...))
714714
}
715715

716716
if upsert, found := test.Operation.Arguments["upsert"]; found {
717-
opts = append(opts, Opt.Upsert(upsert.(bool)))
717+
opts = append(opts, updateopt.Upsert(upsert.(bool)))
718718
}
719719

720720
if collation, found := test.Operation.Arguments["collation"]; found {
721-
opts = append(opts, Opt.Collation(collationFromMap(collation.(map[string]interface{}))))
721+
opts = append(opts, updateopt.Collation(collationFromMap(collation.(map[string]interface{}))))
722722
}
723723

724724
actual, err := coll.UpdateMany(context.Background(), filter, update, opts...)
@@ -763,20 +763,19 @@ func updateOneTest(t *testing.T, coll *Collection, test *testCase) {
763763
replaceFloatsWithInts(filter)
764764
replaceFloatsWithInts(update)
765765

766-
var opts []option.UpdateOptioner
766+
var opts []updateopt.Update
767767

768768
if arrayFilters, found := test.Operation.Arguments["arrayFilters"]; found {
769-
arrayFiltersOpt, err := Opt.ArrayFilters(arrayFilters.([]interface{})...)
770-
require.NoError(t, err)
771-
opts = append(opts, arrayFiltersOpt)
769+
arrayFiltersSlice := arrayFilters.([]interface{})
770+
opts = append(opts, updateopt.ArrayFilters(arrayFiltersSlice...))
772771
}
773772

774773
if upsert, found := test.Operation.Arguments["upsert"]; found {
775-
opts = append(opts, Opt.Upsert(upsert.(bool)))
774+
opts = append(opts, updateopt.Upsert(upsert.(bool)))
776775
}
777776

778777
if collation, found := test.Operation.Arguments["collation"]; found {
779-
opts = append(opts, Opt.Collation(collationFromMap(collation.(map[string]interface{}))))
778+
opts = append(opts, updateopt.Collation(collationFromMap(collation.(map[string]interface{}))))
780779
}
781780

782781
actual, err := coll.UpdateOne(context.Background(), filter, update, opts...)

0 commit comments

Comments
 (0)