diff options
author | Steve Tarzia <steve.tarzia@mongodb.com> | 2022-03-07 23:02:55 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-03-09 20:17:22 +0000 |
commit | 3fe0851620a782f703e722e37afa05460986c81f (patch) | |
tree | 0b560b95198c2db20256b2434f20380388a76e4e | |
parent | 2c7c06c4defd5f56d9b71225917f28397766aa12 (diff) | |
download | mongo-3fe0851620a782f703e722e37afa05460986c81f.tar.gz |
SERVER-62242 fixed $indexOfArray handling of duplicate vals in const arrays
-rw-r--r-- | jstests/aggregation/bugs/server62242.js | 32 | ||||
-rw-r--r-- | src/mongo/db/pipeline/expression.cpp | 6 |
2 files changed, 35 insertions, 3 deletions
diff --git a/jstests/aggregation/bugs/server62242.js b/jstests/aggregation/bugs/server62242.js new file mode 100644 index 00000000000..273dddbf16d --- /dev/null +++ b/jstests/aggregation/bugs/server62242.js @@ -0,0 +1,32 @@ +// SERVER-62242 +// $indexOfArray does not work with duplicate values in array +(function() { +"use strict"; + +const c = db[jsTest.name()]; +c.drop(); + +c.save({_id: 0, number: 222}); + +// before SERVER-62242, this incorrectly returned {_id: 0, number: 222, idx: -1} +assert.eq( + [{_id: 0, number: 222, idx: 2}], + c.aggregate([{$addFields: {idx: {$indexOfArray: [[111, 111, 222], "$number"]}}}]).toArray()); + +// this query was OK +assert.eq([{_id: 0, number: 222, idx: 2}], + c.aggregate([ + {$addFields: {idx: {$indexOfArray: [[111, 111, 222, 333], "$number"]}}} + ]).toArray()); + +// also test for cases where a range is specified +assert.eq([{_id: 0, number: 222, idx: -1}], + c.aggregate([ + {$addFields: {idx: {$indexOfArray: [[111, 111, 222, 333], "$number", 0, 1]}}} + ]).toArray()); + +assert.eq([{_id: 0, number: 222, idx: 3}], + c.aggregate([ + {$addFields: {idx: {$indexOfArray: [[111, 111, 222, 222, 333], "$number", 3, 5]}}} + ]).toArray()); +})(); diff --git a/src/mongo/db/pipeline/expression.cpp b/src/mongo/db/pipeline/expression.cpp index 5a26f521ab8..ea28f3e13cc 100644 --- a/src/mongo/db/pipeline/expression.cpp +++ b/src/mongo/db/pipeline/expression.cpp @@ -2867,14 +2867,14 @@ public: } virtual Value evaluate(const Document& root, Variables* variables) const { - - auto args = evaluateAndValidateArguments(root, _children, _indexMap.size(), variables); + int arraySize = _children[0]->evaluate(root, variables).getArrayLength(); + auto args = evaluateAndValidateArguments(root, _children, arraySize, variables); auto indexVec = _indexMap.find(args.targetOfSearch); if (indexVec == _indexMap.end()) return Value(-1); - // Search through the vector of indecies for first index in our range. + // Search through the vector of indexes for first index in our range. for (auto index : indexVec->second) { if (index >= args.startIndex && index < args.endIndex) { return Value(index); |