From 92d0ebf34f39f82f430869273ae751c1d97ec960 Mon Sep 17 00:00:00 2001 From: Ian Boros Date: Thu, 21 Nov 2019 21:47:37 +0000 Subject: SERVER-14466 test use of DBRef fields in aggregation and find() expressions --- jstests/core/elemMatchProjection.js | 25 +++++++++++++++++++++++-- jstests/core/views/dbref_projection.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 jstests/core/views/dbref_projection.js (limited to 'jstests/core') 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"}}]); +}()); -- cgit v1.2.1