Skip to content

Commit f172d27

Browse files
committed
Fix bug with bson.Document.Set
Fixes a bug with bson.Document.Set where documents that are not in lexicographical order would have elements incorrectly replaced. GODRIVER-339 Change-Id: I37f1415ba10196121229b117f44b1eeb97e0a200
1 parent a731122 commit f172d27

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

bson/document.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (d *Document) Set(elem *Element) *Document {
256256
key := elem.Key() + "\x00"
257257
i := sort.Search(len(d.index), func(i int) bool { return bytes.Compare(d.keyFromIndex(i), []byte(key)) >= 0 })
258258
if i < len(d.index) && bytes.Compare(d.keyFromIndex(i), []byte(key)) == 0 {
259-
d.elems[i] = elem
259+
d.elems[d.index[i]] = elem
260260
return d
261261
}
262262

bson/document_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ func TestDocument(t *testing.T) {
314314
EC.Null("x"),
315315
(&Document{}).Append(EC.Null("w"), EC.Null("y"), EC.Null("z"), EC.Null("x")),
316316
},
317+
{
318+
"update-element-not-lexicographically-sorted",
319+
NewDocument(EC.Int32("b", 1), EC.Int32("a", 2), EC.Int32("d", 3), EC.Int32("c", 4)),
320+
EC.Int32("d", 5),
321+
NewDocument(EC.Int32("b", 1), EC.Int32("a", 2), EC.Int32("d", 5), EC.Int32("c", 4)),
322+
},
317323
}
318324

319325
for _, tc := range testCases {

0 commit comments

Comments
 (0)