summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod
diff options
context:
space:
mode:
authoryarai <yuta.arai@10gen.com>2018-09-12 14:43:56 -0400
committeryarai <yuta.arai@10gen.com>2018-09-21 15:46:34 -0400
commitfc75cfdf2e8f97572efd97a348893ba08a82276a (patch)
tree2a9538ab5b6158a039b7f8be7bd67c25d6f9514b /jstests/noPassthroughWithMongod
parent92b9046433bf8a50dae15f733485adb859d7ed72 (diff)
downloadmongo-fc75cfdf2e8f97572efd97a348893ba08a82276a.tar.gz
SERVER-36931 Handle returnKey option for $** indexes
Diffstat (limited to 'jstests/noPassthroughWithMongod')
-rw-r--r--jstests/noPassthroughWithMongod/all_paths_return_key.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/jstests/noPassthroughWithMongod/all_paths_return_key.js b/jstests/noPassthroughWithMongod/all_paths_return_key.js
new file mode 100644
index 00000000000..75d503b320b
--- /dev/null
+++ b/jstests/noPassthroughWithMongod/all_paths_return_key.js
@@ -0,0 +1,70 @@
+/**
+ * Tests that $** indexes works with returnKey option.
+ * TODO: SERVER-36198: Move this test back to jstests/core/
+ */
+(function() {
+ 'use strict';
+
+ load("jstests/aggregation/extras/utils.js");
+
+ const coll = db.all_paths_return_key;
+ coll.drop();
+
+ const assertArrayEq = (l, r) => assert(arrayEq(l, r), tojson(l) + " != " + tojson(r));
+ const assertArrayNotEq = (l, r) => assert(!arrayEq(l, r), tojson(l) + " == " + tojson(r));
+
+ // Required in order to build $** indexes.
+ assert.commandWorked(
+ db.adminCommand({setParameter: 1, internalQueryAllowAllPathsIndexes: true}));
+
+ try {
+ assert.commandWorked(coll.createIndex({"$**": 1}));
+
+ assert.commandWorked(coll.insert({a: 1, b: 2, c: {d: 2, e: 1}}));
+ assert.commandWorked(coll.insert({a: 2, b: 2, c: {d: 1, e: 2}}));
+ assert.commandWorked(coll.insert({a: 2, b: 1, c: {d: 2, e: 2}}));
+ assert.commandWorked(coll.insert({a: 1, b: 1, c: {e: 2}}));
+
+ // $** index return key with one field argument.
+ assertArrayEq(coll.find({a: 1}).returnKey().toArray(),
+ [{"$_path": "a", a: 1}, {"$_path": "a", a: 1}]);
+
+ // $** index return key with dot path argument.
+ assertArrayEq(coll.find({"c.e": 1}).returnKey().toArray(), [{"$_path": "c.e", "c.e": 1}]);
+
+ assert.commandWorked(coll.createIndex({"a": 1}));
+
+ // $** index return key with competing regular index.
+ assertArrayEq(coll.find({a: 1}).hint({"$**": 1}).returnKey().toArray(),
+ [{"$_path": "a", a: 1}, {"$_path": "a", a: 1}]);
+
+ assert.commandWorked(coll.createIndex({"a": 1, "b": 1}));
+
+ // $** index return key with competing compound index.
+ assertArrayNotEq(coll.find({a: 1, b: 1}).hint({"$**": 1}).returnKey().toArray(),
+ [{a: 1, b: 1}]);
+
+ assert.commandWorked(coll.insert({a: 2, b: 2, c: {e: 2}, f: [1, 2, 3]}));
+ assert.commandWorked(coll.insert({a: 2, b: 2, c: {e: 2}, g: [{h: 1}, {i: 2}]}));
+
+ // Multikey path $** index return key.
+ assertArrayEq(coll.find({f: 1}).returnKey().toArray(), [{"$_path": "f", f: 1}]);
+
+ // Multikey subobject $** index return key.
+ assertArrayEq(coll.find({"g.h": 1}).returnKey().toArray(), [{"$_path": "g.h", "g.h": 1}]);
+
+ assert.commandWorked(coll.dropIndexes());
+ assert.commandWorked(coll.createIndex({"c.$**": 1}));
+
+ // Path specified $** index return key.
+ assertArrayEq(coll.find({"c.d": 1}).returnKey().toArray(), [{"$_path": "c.d", "c.d": 1}]);
+
+ // Path specified $** index return key with irrelevant query. We expect this query to be
+ // answered with a collection scan, in which case returnKey is expected to return empty
+ // objects.
+ assertArrayEq(coll.find({a: 1, b: 1}).returnKey().toArray(), [{}]);
+ } finally {
+ // Disable $** indexes once the tests have either completed or failed.
+ db.adminCommand({setParameter: 1, internalQueryAllowAllPathsIndexes: false});
+ }
+})(); \ No newline at end of file