summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
authorDavid Storch <david.storch@mongodb.com>2020-01-10 17:47:50 +0000
committerA. Jesse Jiryu Davis <jesse@mongodb.com>2020-01-27 15:38:03 -0500
commitf0896626cfb7eb32cdcec322c24706293eed865e (patch)
tree86c7efd2b8defcdc972b44feb42b76620e253080 /jstests
parent47882b0ac13860c74587a846edb08533eceb2eb4 (diff)
downloadmongo-f0896626cfb7eb32cdcec322c24706293eed865e.tar.gz
SERVER-37791 Prevent exclusion of non-existent field from affecting field order.
Exclusion of an existing field can still affect the field order produced by subsequent $project or $addFields stages. Changing this is left as future work.
Diffstat (limited to 'jstests')
-rw-r--r--jstests/aggregation/bugs/exclusion_projection_does_not_affect_field_order.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/jstests/aggregation/bugs/exclusion_projection_does_not_affect_field_order.js b/jstests/aggregation/bugs/exclusion_projection_does_not_affect_field_order.js
new file mode 100644
index 00000000000..c781f1ede6d
--- /dev/null
+++ b/jstests/aggregation/bugs/exclusion_projection_does_not_affect_field_order.js
@@ -0,0 +1,33 @@
+// Test that excluding a non-existent field does not affect the field order produced by subsequent
+// changes.
+//
+// This is designed as a regression test for SERVER-37791.
+//
+// @tags: [requires_fcv_44]
+(function() {
+"use strict";
+
+const coll = db.exclusion_projection_does_not_affect_field_order;
+coll.drop();
+
+assert.commandWorked(coll.insert({_id: 1}));
+assert.commandWorked(coll.insert({_id: 2, c: 1}));
+assert.commandWorked(coll.insert({_id: 3, y: 1, z: 1}));
+
+// We expect $addFields to retain the position of pre-existing fields, and then append new fields in
+// the order that they are specified in the query. This rule should not be impacted by the presence
+// of a preceding exclusion projection.
+assert.eq(
+ [
+ {_id: 1, x: 3, y: 4, b: 5, c: 6, a: 7},
+ // Here "c" retains the position that it had prior to being excluded.
+ {_id: 2, c: 6, x: 3, y: 4, b: 5, a: 7},
+ {_id: 3, y: 4, z: 1, x: 3, b: 5, c: 6, a: 7}
+ ],
+ coll.aggregate([
+ {$project: {b: 0, c: 0}},
+ {$addFields: {x: 3, y: 4, b: 5, c: 6, a: 7}},
+ {$sort: {_id: 1}}
+ ])
+ .toArray());
+}());