diff options
author | Arun Banala <arun.banala@10gen.com> | 2019-10-29 17:30:25 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-10-29 17:30:25 +0000 |
commit | 560a25757f56fdb93c018793e8731c2e50bda70b (patch) | |
tree | f4d9f2e3631add1e3bb7f70b18734abbe8578dba | |
parent | 77762fc6d99f29930b6a9582c35c2669b6aec5bd (diff) | |
download | mongo-560a25757f56fdb93c018793e8731c2e50bda70b.tar.gz |
SERVER-44050 Arrays are not correctly rejected during key generation for 'hashed' indexes
(cherry picked from commit 888f7e6fc10ccb999be203b8cbad4dbe19d0a5d2)
(cherry picked from commit ffda4b8dd699251f487596ff008133830a5ec392)
-rw-r--r-- | jstests/core/hashindex1.js | 10 | ||||
-rw-r--r-- | src/mongo/db/index/expression_keys_private.cpp | 20 |
2 files changed, 22 insertions, 8 deletions
diff --git a/jstests/core/hashindex1.js b/jstests/core/hashindex1.js index ef94f32da08..3779b524055 100644 --- a/jstests/core/hashindex1.js +++ b/jstests/core/hashindex1.js @@ -92,3 +92,13 @@ var total = t.find().hint({"_id": 1}).toArray().length; var totala = t.find().hint(goodspec).toArray().length; assert.eq(total, totala, "non-sparse index has wrong total"); assert.lt(totalb, totala, "sparse index should have smaller total"); + +// Test that having arrays along the path of the index is not allowed. +assert.commandWorked(t.createIndex({"field1.field2.0.field4": "hashed"})); +assert.commandFailedWithCode(t.insert({field1: []}), 16766); +assert.commandFailedWithCode(t.insert({field1: {field2: []}}), 16766); +assert.commandFailedWithCode(t.insert({field1: {field2: {0: []}}}), 16766); +assert.commandFailedWithCode(t.insert({field1: [{field2: {0: []}}]}), 16766); +assert.commandFailedWithCode(t.insert({field1: {field2: {0: {field4: []}}}}), 16766); +assert.commandWorked(t.insert({field1: {field2: {0: {otherField: []}}}})); +assert.commandWorked(t.insert({field1: {field2: {0: {field4: 1}}}})); diff --git a/src/mongo/db/index/expression_keys_private.cpp b/src/mongo/db/index/expression_keys_private.cpp index f53cb9050cf..424db842960 100644 --- a/src/mongo/db/index/expression_keys_private.cpp +++ b/src/mongo/db/index/expression_keys_private.cpp @@ -347,7 +347,18 @@ void ExpressionKeysPrivate::getHashKeys(const BSONObj& obj, const CollatorInterface* collator, BSONObjSet* keys) { const char* cstr = hashedField.c_str(); - BSONElement fieldVal = dps::extractElementAtPath(obj, cstr); + BSONElement fieldVal = dps::extractElementAtPathOrArrayAlongPath(obj, cstr); + + // If we hit an array while traversing the path, 'cstr' will point to the path component + // immediately following the array, or the null termination byte if the final field in the path + // was an array. + uassert( + 16766, + str::stream() + << "Error: hashed indexes do not currently support array values. Found array at path: " + << hashedField.substr( + 0, hashedField.size() - StringData(cstr).size() - !StringData(cstr).empty()), + fieldVal.type() != BSONType::Array); // Convert strings to comparison keys. BSONObj fieldValObj; @@ -356,13 +367,6 @@ void ExpressionKeysPrivate::getHashKeys(const BSONObj& obj, CollationIndexKey::collationAwareIndexKeyAppend(fieldVal, collator, &bob); fieldValObj = bob.obj(); fieldVal = fieldValObj.firstElement(); - } - - uassert(16766, - "Error: hashed indexes do not currently support array values", - fieldVal.type() != Array); - - if (!fieldVal.eoo()) { BSONObj key = BSON("" << makeSingleHashKey(fieldVal, seed, hashVersion)); keys->insert(key); } else if (!isSparse) { |