summaryrefslogtreecommitdiff
path: root/jstests/core
diff options
context:
space:
mode:
authorIan Boros <ian.boros@mongodb.com>2019-11-21 21:47:37 +0000
committerevergreen <evergreen@mongodb.com>2019-11-21 21:47:37 +0000
commit92d0ebf34f39f82f430869273ae751c1d97ec960 (patch)
tree7f9e0c615cb660318dd6cddd719d18c6baadbd28 /jstests/core
parentda88c83317eb298dad14c834140ce39fb63ff6d5 (diff)
downloadmongo-92d0ebf34f39f82f430869273ae751c1d97ec960.tar.gz
SERVER-14466 test use of DBRef fields in aggregation and find() expressions
Diffstat (limited to 'jstests/core')
-rw-r--r--jstests/core/elemMatchProjection.js25
-rw-r--r--jstests/core/views/dbref_projection.js30
2 files changed, 53 insertions, 2 deletions
diff --git a/jstests/core/elemMatchProjection.js b/jstests/core/elemMatchProjection.js
index 1e6632ea2a5..1bf4fe11217 100644
--- a/jstests/core/elemMatchProjection.js
+++ b/jstests/core/elemMatchProjection.js
@@ -1,5 +1,4 @@
-// @tags: [requires_getmore]
-
+// @tags: [requires_getmore, requires_fcv_44]
// Tests for $elemMatch projections and $ positional operator projection.
(function() {
"use strict";
@@ -65,6 +64,18 @@ for (let i = 0; i < 100; i++) {
bulk.insert({_id: nextId(), group: 12, x: {y: [{a: 1, b: 1}, {a: 1, b: 2}]}});
bulk.insert({_id: nextId(), group: 13, x: [{a: 1, b: 1}, {a: 1, b: 2}]});
bulk.insert({_id: nextId(), group: 13, x: [{a: 1, b: 2}, {a: 1, b: 1}]});
+
+ // Array of DBRefs. Don't actually try to dereference them, though, as they point to
+ // non-existing collections.
+ bulk.insert({
+ _id: nextId(),
+ group: 14,
+ x: [
+ new DBRef("otherCollection", "id0", db.getName()),
+ new DBRef("otherCollection", "id1", db.getName()),
+ new DBRef("otherCollection2", "id2", db.getName())
+ ]
+ });
}
assert.commandWorked(bulk.execute());
@@ -217,6 +228,16 @@ assert.eq({"x": [{"a": 1, "b": 2}], "y": [{"c": 3, "d": 4}]},
.toArray()[0],
"multiple $elemMatch on unique fields 1");
+// Perform a $elemMatch on a DBRef field.
+assert.eq(coll.find({group: 14}, {x: {$elemMatch: {$id: "id0"}}}).toArray()[0].x,
+ [new DBRef("otherCollection", "id0", db.getName())]);
+
+assert.eq(coll.find({group: 14}, {x: {$elemMatch: {$ref: "otherCollection2"}}}).toArray()[0].x,
+ [new DBRef("otherCollection2", "id2", db.getName())]);
+
+assert.eq(coll.find({group: 14}, {x: {$elemMatch: {$db: db.getName()}}}).toArray()[0].x,
+ [new DBRef("otherCollection", "id0", db.getName())]);
+
// Tests involving getMore. Test the $-positional operator across multiple batches.
let a = coll.find({group: 3, 'x.b': 2}, {'x.$': 1}).sort({_id: 1}).batchSize(1);
while (a.hasNext()) {
diff --git a/jstests/core/views/dbref_projection.js b/jstests/core/views/dbref_projection.js
new file mode 100644
index 00000000000..963a3cc185e
--- /dev/null
+++ b/jstests/core/views/dbref_projection.js
@@ -0,0 +1,30 @@
+/**
+ * Test projecting DBRef fields ($ref, $id, $db) in views.
+ *
+ * Legacy find() queries do not support views, so must use the find() command.
+ * DBRef fields are not supported in agg pre 4.4.
+ * @tags: [requires_find_command, requires_fcv_44]
+ */
+(function() {
+"use strict";
+
+const viewsDB = db.getSiblingDB("views_dbref_projection");
+assert.commandWorked(viewsDB.dropDatabase());
+
+assert.commandWorked(
+ viewsDB.baseColl.insert({_id: 0, link: new DBRef("otherColl", "someId", viewsDB.getName())}));
+
+assert.commandWorked(viewsDB.runCommand({create: "view", viewOn: "baseColl"}));
+
+// Check that the view and base collection return the same thing.
+function checkViewAndBaseCollection(projection, expectedResult) {
+ const baseRes = viewsDB.baseColl.find({}, projection).toArray();
+ const viewRes = viewsDB.view.find({}, projection).toArray();
+ assert.eq(baseRes, viewRes);
+ assert.eq(expectedResult, baseRes);
+}
+
+checkViewAndBaseCollection({"link.$ref": 1}, [{_id: 0, link: {$ref: "otherColl"}}]);
+checkViewAndBaseCollection({"link.$db": 1}, [{_id: 0, link: {$db: viewsDB.getName()}}]);
+checkViewAndBaseCollection({"link.$id": 1}, [{_id: 0, link: {$id: "someId"}}]);
+}());