diff options
-rw-r--r-- | jstests/multiVersion/hashed_index_bad_keys_cleanup.js | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/jstests/multiVersion/hashed_index_bad_keys_cleanup.js b/jstests/multiVersion/hashed_index_bad_keys_cleanup.js index 39675fdb730..87785a023b0 100644 --- a/jstests/multiVersion/hashed_index_bad_keys_cleanup.js +++ b/jstests/multiVersion/hashed_index_bad_keys_cleanup.js @@ -43,6 +43,19 @@ assert.commandWorked(coll.insert({_id: 3, p: [{q: 1}]})); assert.commandWorked(coll.insert({_id: 4, a: 1, p: [{q: 1}]})); assert.commandWorked(coll.insert({_id: 5, a: 1, p: [{q: 1}]})); +// Assert that the collection has expected number of documents and index keys. +function assertCollectionHasExpectedDocs(expectedNumDocs) { + const collState = { + documents: coll.find().toArray(), + indexKeys: coll.find().hint({"p.q.r": "hashed"}).returnKey().toArray() + }; + assert.eq(collState.documents.length, expectedNumDocs, collState); + assert.eq(collState.indexKeys.length, expectedNumDocs, collState); +} + +// Verify that the documents inserted have the corresponding index keys. +assertCollectionHasExpectedDocs(5); + // Helper function which runs validate() on primary and secondary nodes, then verifies that the // command returned the expected result. function assertValidateCmdReturned(expectedResult) { @@ -63,9 +76,8 @@ rst.upgradeSet(nodeOptionsOfLatestVersion); testDB = rst.getPrimary().getDB(jsTestName()); coll = testDB.coll; -// Verify that the five documents inserted earlier have their index keys. -let res = coll.find().hint({"p.q.r": "hashed"}).returnKey().itcount(); -assert.eq(res, 5); +// Verify that the five documents inserted earlier have their index keys after upgrade. +assertCollectionHasExpectedDocs(5); // Verify that after upgrade, inserting bad documents is not allowed. const arrayAlongPathFailCode = 16766; @@ -101,7 +113,7 @@ assert.commandFailedWithCode(testDB.runCommand({ assert.commandWorked(coll.update({_id: 2}, {p: {q: {r: 4}}})); // Verify that the index key is updated correctly by quering with hashed index. -res = coll.find({"p.q.r": 4}).hint({"p.q.r": "hashed"}).toArray(); +let res = coll.find({"p.q.r": 4}).hint({"p.q.r": "hashed"}).toArray(); assert.eq(res, [{_id: 2, p: {q: {r: 4}}}]); // Validate should still fail since a bad document {_id: 3} exists. |