Skip to content

Commit

Permalink
Make $pullAll remove all instances from the existing array (FerretD…
Browse files Browse the repository at this point in the history
  • Loading branch information
b1ron authored Mar 9, 2023
1 parent cf86bdf commit ffece7e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion integration/query_array_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func testQueryArrayCompatAll() map[string]queryCompatTestCase {
filter: bson.D{{"v", bson.D{{"$all", bson.A{int32(42)}}}}},
},
"WholeNotFound": {
filter: bson.D{{"v", bson.D{{"$all", bson.A{int32(44)}}}}},
filter: bson.D{{"v", bson.D{{"$all", bson.A{int32(46)}}}}},
resultType: emptyResult,
},
"Zero": {
Expand Down
3 changes: 3 additions & 0 deletions integration/shareddata/composites.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ var ArrayInt32s = &Values[string]{
"array-int32-three": bson.A{int32(42), int32(43), int32(42)},
// "array-int32-nil": nil, TODO: https://github.com/FerretDB/FerretDB/issues/1836
"array-int32-empty": bson.A{},
"array-int32-six": bson.A{
int32(42), int32(43), int32(44), int32(45), int32(42), int32(43),
},
},
}

Expand Down
3 changes: 3 additions & 0 deletions integration/update_array_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ func TestUpdateArrayCompatPullAll(t *testing.T) {
"Int32": {
update: bson.D{{"$pullAll", bson.D{{"v", bson.A{int32(42)}}}}},
},
"Int32-Six-Elements": {
update: bson.D{{"$pullAll", bson.D{{"v", bson.A{int32(42), int32(43)}}}}},
},
"Int64": {
update: bson.D{{"$pullAll", bson.D{{"v", bson.A{int64(42)}}}}},
},
Expand Down
22 changes: 8 additions & 14 deletions internal/handlers/common/update_array_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,34 +374,28 @@ func processPullAllArrayUpdateExpression(doc, update *types.Document) (bool, err
)
}

for i := 0; i < array.Len(); {
var value any
for j := 0; j < pullAllArray.Len(); j++ {
var valueToPull any

value, err = array.Get(i)
valueToPull, err = pullAllArray.Get(j)
if err != nil {
return false, lazyerrors.Error(err)
}

for j := 0; j < pullAllArray.Len(); j++ {
var valueToPull any
// we remove all instances of valueToPull in array
for i := array.Len() - 1; i >= 0; i-- {
var value any

valueToPull, err = pullAllArray.Get(j)
value, err = array.Get(i)
if err != nil {
return false, lazyerrors.Error(err)
}

compareResult := types.Compare(value, valueToPull)

if compareResult == types.Equal {
if types.Compare(value, valueToPull) == types.Equal {
array.Remove(i)

changed = true

continue
}

// Increment i only if the value was not removed.
i++
}
}

Expand Down

0 comments on commit ffece7e

Please sign in to comment.