Skip to content

Commit

Permalink
fix #224 (#258)
Browse files Browse the repository at this point in the history
* fix #224

* fix #242
  • Loading branch information
jiangz222 authored Oct 4, 2022
1 parent 19f1bba commit d076e1b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 4 additions & 1 deletion collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ func (c *Collection) UpdateOne(ctx context.Context, filter interface{}, update i

res, err := c.collection.UpdateOne(ctx, filter, update, updateOpts)
if res != nil && res.MatchedCount == 0 {
err = ErrNoSuchDocuments
// UpdateOne support upsert function
if updateOpts.Upsert == nil || !*updateOpts.Upsert {
err = ErrNoSuchDocuments
}
}
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ func TestCollection_Update(t *testing.T) {
err = cli.UpdateOne(context.Background(), filter2, update2)
ast.Equal(err, ErrNoSuchDocuments)

opt := officialOpts.Update().SetUpsert(true)
opts = options.UpdateOptions{UpdateOptions: opt}
err = cli.UpdateOne(context.Background(), filter2, update2, opts)
ast.NoError(err)

// filter is nil or wrong BSON Document format
update3 := bson.M{
"name": "Geek",
Expand Down
4 changes: 2 additions & 2 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func Register(cb callback) {

// Do call every registers
// The doc is always the document to operate
func Do(ctx context.Context, doc interface{}, opType operator.OpType, opts ...interface{}) error {
func Do(ctx context.Context, content interface{}, opType operator.OpType, opts ...interface{}) error {
for _, cb := range middlewareCallback {
if err := cb(ctx, doc, opType, opts...); err != nil {
if err := cb(ctx, content, opType, opts...); err != nil {
return err
}
}
Expand Down

0 comments on commit d076e1b

Please sign in to comment.