Skip to content

Commit d076e1b

Browse files
authored
fix #224 (#258)
* fix #224 * fix #242
1 parent 19f1bba commit d076e1b

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

collection.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ func (c *Collection) UpdateOne(ctx context.Context, filter interface{}, update i
216216

217217
res, err := c.collection.UpdateOne(ctx, filter, update, updateOpts)
218218
if res != nil && res.MatchedCount == 0 {
219-
err = ErrNoSuchDocuments
219+
// UpdateOne support upsert function
220+
if updateOpts.Upsert == nil || !*updateOpts.Upsert {
221+
err = ErrNoSuchDocuments
222+
}
220223
}
221224
if err != nil {
222225
return err

collection_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@ func TestCollection_Update(t *testing.T) {
436436
err = cli.UpdateOne(context.Background(), filter2, update2)
437437
ast.Equal(err, ErrNoSuchDocuments)
438438

439+
opt := officialOpts.Update().SetUpsert(true)
440+
opts = options.UpdateOptions{UpdateOptions: opt}
441+
err = cli.UpdateOne(context.Background(), filter2, update2, opts)
442+
ast.NoError(err)
443+
439444
// filter is nil or wrong BSON Document format
440445
update3 := bson.M{
441446
"name": "Geek",

middleware/middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func Register(cb callback) {
2626

2727
// Do call every registers
2828
// The doc is always the document to operate
29-
func Do(ctx context.Context, doc interface{}, opType operator.OpType, opts ...interface{}) error {
29+
func Do(ctx context.Context, content interface{}, opType operator.OpType, opts ...interface{}) error {
3030
for _, cb := range middlewareCallback {
31-
if err := cb(ctx, doc, opType, opts...); err != nil {
31+
if err := cb(ctx, content, opType, opts...); err != nil {
3232
return err
3333
}
3434
}

0 commit comments

Comments
 (0)