summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
Diffstat (limited to 'jstests')
-rw-r--r--jstests/noPassthroughWithMongod/projection_optimizations.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/jstests/noPassthroughWithMongod/projection_optimizations.js b/jstests/noPassthroughWithMongod/projection_optimizations.js
new file mode 100644
index 00000000000..57469299eb1
--- /dev/null
+++ b/jstests/noPassthroughWithMongod/projection_optimizations.js
@@ -0,0 +1,33 @@
+/**
+ * Test projections with $and in cases where optimizations could be performed.
+ */
+(function() {
+"use strict";
+
+load("jstests/aggregation/extras/utils.js");
+load('jstests/libs/analyze_plan.js');
+
+const coll = db.projection_and;
+coll.drop();
+
+assert.commandWorked(coll.insert({_id: 0, a: 1}));
+assert.commandWorked(coll.createIndex({a: 1}));
+
+function runFindWithProjection({filter = {}, projection, expected} = {}) {
+ const res = coll.find(filter, projection);
+ assertArrayEq(res.toArray(), expected);
+ return res;
+}
+
+let result = runFindWithProjection({
+ filter: {a: 1},
+ projection: {_id: 0, a: 1, b: {$and: [false, "$b"]}},
+ expected: [{a: 1, b: false}]
+});
+// Query should be optimized and covered.
+assert(isIndexOnly(db, getWinningPlan(result.explain().queryPlanner)));
+
+result = runFindWithProjection(
+ {projection: {a: {$and: ['$a', true, 1]}}, expected: [{_id: 0, a: true}]});
+assert(isCollscan(db, getWinningPlan(result.explain().queryPlanner)));
+})();