summaryrefslogtreecommitdiff
path: root/jstests/core/wildcard_index_return_key.js
diff options
context:
space:
mode:
authorBernard Gorman <bernard.gorman@gmail.com>2018-10-06 11:28:49 +0100
committerBernard Gorman <bernard.gorman@gmail.com>2018-10-10 16:03:27 +0100
commit1b51df8420ef3c57afb2d753d7188814e90eee51 (patch)
treecdb3247e7ea95521c5b4ac1330341a13b9713622 /jstests/core/wildcard_index_return_key.js
parent549616025c9d471767bc7ede342a9919cb3e6f94 (diff)
downloadmongo-1b51df8420ef3c57afb2d753d7188814e90eee51.tar.gz
SERVER-31698 Move wildcard_index integration tests to /core/
Diffstat (limited to 'jstests/core/wildcard_index_return_key.js')
-rw-r--r--jstests/core/wildcard_index_return_key.js59
1 files changed, 59 insertions, 0 deletions
diff --git a/jstests/core/wildcard_index_return_key.js b/jstests/core/wildcard_index_return_key.js
new file mode 100644
index 00000000000..ceaf691aad8
--- /dev/null
+++ b/jstests/core/wildcard_index_return_key.js
@@ -0,0 +1,59 @@
+/**
+ * Tests that $** indexes works with returnKey option.
+ */
+(function() {
+ 'use strict';
+
+ load("jstests/aggregation/extras/utils.js");
+
+ const coll = db.wildcard_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));
+
+ 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 COLLSCAN, in which case returnKey is expected to return empty objects.
+ assertArrayEq(coll.find({a: 1, b: 1}).returnKey().toArray(), [{}]);
+})(); \ No newline at end of file