Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #244 #242 #258

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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