Skip to content

Commit 7f6139f

Browse files
authored
Merge pull request #96 from qnxm/develop
fix Apply func return err
2 parents 13372c8 + d9d46e0 commit 7f6139f

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

query.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,12 @@ func (q *Query) findOneAndReplace(change Change, result interface{}) error {
341341
opts.SetReturnDocument(options.After)
342342
}
343343

344-
return q.collection.FindOneAndReplace(q.ctx, q.filter, change.Update, opts).Decode(result)
344+
err := q.collection.FindOneAndReplace(q.ctx, q.filter, change.Update, opts).Decode(result)
345+
if change.Upsert && !change.ReturnNew && err == mongo.ErrNoDocuments {
346+
return nil
347+
}
348+
349+
return err
345350
}
346351

347352
// findOneAndUpdate
@@ -361,5 +366,10 @@ func (q *Query) findOneAndUpdate(change Change, result interface{}) error {
361366
opts.SetReturnDocument(options.After)
362367
}
363368

364-
return q.collection.FindOneAndUpdate(q.ctx, q.filter, change.Update, opts).Decode(result)
369+
err := q.collection.FindOneAndUpdate(q.ctx, q.filter, change.Update, opts).Decode(result)
370+
if change.Upsert && !change.ReturnNew && err == mongo.ErrNoDocuments {
371+
return nil
372+
}
373+
374+
return err
365375
}

query_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,25 @@ func TestQuery_Apply(t *testing.T) {
608608
err = cli.Find(context.Background(), filter1).Apply(change1, &res1)
609609
ast.EqualError(err, mongo.ErrNoDocuments.Error())
610610

611+
change1.ReturnNew = false
612+
change1.Upsert = true
613+
err = cli.Find(context.Background(), filter1).Apply(change1, &res1)
614+
ast.NoError(err)
615+
ast.Equal( "", res1.Name)
616+
ast.Equal(0, res1.Age)
617+
618+
change1.Update = bson.M{
619+
operator.Set: bson.M{
620+
"name": "Tom",
621+
"age": 19,
622+
},
623+
}
624+
change1.ReturnNew = true
611625
change1.Upsert = true
612626
err = cli.Find(context.Background(), filter1).Apply(change1, &res1)
613627
ast.NoError(err)
614628
ast.Equal( "Tom", res1.Name)
615-
ast.Equal(18, res1.Age)
629+
ast.Equal(19, res1.Age)
616630

617631
res2 := QueryTestItem{}
618632
filter2 := bson.M{
@@ -677,6 +691,10 @@ func TestQuery_Apply(t *testing.T) {
677691
err = cli.Find(context.Background(), filter4).Apply(change4, &res4)
678692
ast.EqualError(err, mongo.ErrNoDocuments.Error())
679693

694+
change4.ReturnNew = true
695+
err = cli.Find(context.Background(), filter4).Apply(change4, &res4)
696+
ast.EqualError(err, mongo.ErrNoDocuments.Error())
697+
680698
change4.Upsert = true
681699
change4.ReturnNew = true
682700
err = cli.Find(context.Background(), filter4).Apply(change4, &res4)
@@ -698,4 +716,20 @@ func TestQuery_Apply(t *testing.T) {
698716
ast.NoError(err)
699717
ast.Equal("Bob", res4.Name)
700718
ast.Equal(23, res4.Age)
719+
720+
721+
res4 = QueryTestItem{}
722+
filter4 = bson.M{
723+
"name": "James",
724+
}
725+
change4 = Change{
726+
Replace: true,
727+
Update: bson.M{"name": "James", "age": 26},
728+
Upsert: true,
729+
ReturnNew: false,
730+
}
731+
err = cli.Find(context.Background(), filter4).Apply(change4, &res4)
732+
ast.NoError(err)
733+
ast.Equal("", res4.Name)
734+
ast.Equal(0, res4.Age)
701735
}

0 commit comments

Comments
 (0)