diff options
author | James Wahlin <james@mongodb.com> | 2018-10-01 16:05:08 -0400 |
---|---|---|
committer | James Wahlin <james@mongodb.com> | 2018-10-02 09:56:37 -0400 |
commit | f7ddb89c8caf438781de9191d690d03f711b63fe (patch) | |
tree | 9244266869d8c17de23d5e0c817a2fddc2bc849a /jstests/noPassthroughWithMongod/wildcard_index_nonblocking_sort.js | |
parent | 5a6ee566f608c46654133de17a4e8bb3464d680d (diff) | |
download | mongo-f7ddb89c8caf438781de9191d690d03f711b63fe.tar.gz |
SERVER-37188 Rename "All Paths" index to "Wildcard" index
Diffstat (limited to 'jstests/noPassthroughWithMongod/wildcard_index_nonblocking_sort.js')
-rw-r--r-- | jstests/noPassthroughWithMongod/wildcard_index_nonblocking_sort.js | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/jstests/noPassthroughWithMongod/wildcard_index_nonblocking_sort.js b/jstests/noPassthroughWithMongod/wildcard_index_nonblocking_sort.js new file mode 100644 index 00000000000..b9c7edb3921 --- /dev/null +++ b/jstests/noPassthroughWithMongod/wildcard_index_nonblocking_sort.js @@ -0,0 +1,67 @@ +(function() { + "use strict"; + + load("jstests/aggregation/extras/utils.js"); // For arrayEq(). + load("jstests/libs/analyze_plan.js"); // For getPlanStages(). + + const coll = db.wildcard_nonblocking_sort; + + // Required in order to build $** indexes. + assert.commandWorked( + db.adminCommand({setParameter: 1, internalQueryAllowAllPathsIndexes: true})); + + assert.commandWorked(coll.createIndex({"$**": 1}, {wildcardProjection: {"excludedField": 0}})); + + for (let i = 0; i < 50; i++) { + assert.commandWorked(coll.insert({a: i, b: -i, x: [123], excludedField: i})); + } + + function checkQueryHasSameResultsWhenUsingIdIndex(query, sort) { + const l = coll.find(query).sort(sort).toArray(); + const r = coll.find(query).sort(sort).hint({$natural: 1}).toArray(); + assert(arrayEq(l, r)); + } + + function checkQueryUsesSortType(query, sort, isBlocking) { + const explain = assert.commandWorked(coll.find(query).sort(sort).explain()); + const plan = explain.queryPlanner.winningPlan; + + const ixScans = getPlanStages(plan, "IXSCAN"); + const sorts = getPlanStages(plan, "SORT"); + + if (isBlocking) { + assert.eq(sorts.length, 1); + assert.eq(sorts[0].sortPattern, sort); + + // A blocking sort may or may not use the index, so we don't check the length of + // 'ixScans'. + } else { + assert.eq(sorts.length, 0); + assert.eq(ixScans.length, 1); + + const sortKey = Object.keys(sort)[0]; + assert.docEq(ixScans[0].keyPattern, {$_path: 1, [sortKey]: 1}); + } + } + + function checkQueryUsesNonBlockingSortAndGetsCorrectResults(query, sort) { + checkQueryUsesSortType(query, sort, false); + checkQueryHasSameResultsWhenUsingIdIndex(query, sort); + } + + function checkQueryUsesBlockingSortAndGetsCorrectResults(query, sort) { + checkQueryUsesSortType(query, sort, true); + checkQueryHasSameResultsWhenUsingIdIndex(query, sort); + } + + checkQueryUsesNonBlockingSortAndGetsCorrectResults({a: {$gte: 0}}, {a: 1}); + checkQueryUsesNonBlockingSortAndGetsCorrectResults({a: {$gte: 0}, x: 123}, {a: 1}); + + checkQueryUsesBlockingSortAndGetsCorrectResults({x: {$elemMatch: {$eq: 123}}}, {x: 1}); + checkQueryUsesBlockingSortAndGetsCorrectResults({x: {$elemMatch: {$eq: 123}}}, {a: 1}); + checkQueryUsesBlockingSortAndGetsCorrectResults({a: {$gte: 0}}, {a: 1, b: 1}); + checkQueryUsesBlockingSortAndGetsCorrectResults({a: {$exists: true}}, {a: 1}); + checkQueryUsesBlockingSortAndGetsCorrectResults({}, {a: 1}); + checkQueryUsesBlockingSortAndGetsCorrectResults({x: 123}, {a: 1}); + checkQueryUsesBlockingSortAndGetsCorrectResults({excludedField: {$gte: 0}}, {excludedField: 1}); +})(); |