[GODRIVER-732] ReplaceOne result does not set UpsertedID and possibly MatchedCount is wrong Created: 20/Dec/18  Updated: 27/Oct/23  Resolved: 14/Jan/19

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: No Release
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Asaf Shakarzy Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

ReplaceOne perform as expected but UpdateResult is wrong:

 

  • MatchedCount always return 1 (possibly ok but logically should return all matched docs)
  • UpsertedID is nil

 

Consider this sampel code:

client, _ := mongo.NewClient("mongodb://localhost:27017")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err := client.Connect(ctx)
assert.Nil(t, err)
 
c := client.Database("testing").Collection("c1")
 
d1 := bson.M{"name": "pi", "value": 3.14159}
d2 := bson.M{"name": "pi", "value": 3.14159}
r, err := c.InsertMany(ctx, []interface{}{d1, d2})
assert.Nil(t, err)
assert.Len(t, r.InsertedIDs, 2)
 
ror, err := c.ReplaceOne(ctx, nil, bson.M{"name": "pi", "value": -1})
assert.Nil(t, err)
// (1) MatchedCount expected to be 2
fmt.Println(ror.MatchedCount)
// (2) Expected to return the ID of the updated document but returns nil
fmt.Println(ror.UpsertedID)
 
c.Drop(ctx)
 

 

 

Thanks.



 Comments   
Comment by Kristofer Brandow (Inactive) [ 14/Jan/19 ]

Hi asaf000,

You need to add the Upsert option to enable ReplaceOne to upsert.

--Kris

Generated at Thu Feb 08 08:34:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.