summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/addFields/use_cases.js
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/aggregation/sources/addFields/use_cases.js')
-rw-r--r--jstests/aggregation/sources/addFields/use_cases.js93
1 files changed, 46 insertions, 47 deletions
diff --git a/jstests/aggregation/sources/addFields/use_cases.js b/jstests/aggregation/sources/addFields/use_cases.js
index b6f92fdb7b7..2f6f454ba5a 100644
--- a/jstests/aggregation/sources/addFields/use_cases.js
+++ b/jstests/aggregation/sources/addFields/use_cases.js
@@ -9,60 +9,59 @@
*/
(function() {
- "use strict";
+"use strict";
- // For arrayEq.
- load("jstests/aggregation/extras/utils.js");
+// For arrayEq.
+load("jstests/aggregation/extras/utils.js");
- const dbName = "test";
- const collName = jsTest.name();
+const dbName = "test";
+const collName = jsTest.name();
- function doExecutionTest(conn) {
- const coll = conn.getDB(dbName).getCollection(collName);
- coll.drop();
+function doExecutionTest(conn) {
+ const coll = conn.getDB(dbName).getCollection(collName);
+ coll.drop();
- // Insert a bunch of documents of the form above.
- const nDocs = 10;
- for (let i = 0; i < nDocs; i++) {
- assert.writeOK(coll.insert({"_id": i, "2i": i * 2, "3i": i * 3}));
- }
-
- // Add the minimum, maximum, and average temperatures, and make sure that doing the same
- // with addFields yields the correct answer.
- // First compute with $project, since we know all the fields in this document.
- let projectPipe = [{
- $project: {
- "2i": 1,
- "3i": 1,
- "6i^2": {"$multiply": ["$2i", "$3i"]},
- // _id is implicitly included.
- }
- }];
- let correct = coll.aggregate(projectPipe).toArray();
+ // Insert a bunch of documents of the form above.
+ const nDocs = 10;
+ for (let i = 0; i < nDocs; i++) {
+ assert.writeOK(coll.insert({"_id": i, "2i": i * 2, "3i": i * 3}));
+ }
- // Then compute the same results using $addFields.
- let addFieldsPipe = [{
- $addFields: {
- "6i^2": {"$multiply": ["$2i", "$3i"]},
- // All other fields are implicitly included.
- }
- }];
- let addFieldsResult = coll.aggregate(addFieldsPipe).toArray();
+ // Add the minimum, maximum, and average temperatures, and make sure that doing the same
+ // with addFields yields the correct answer.
+ // First compute with $project, since we know all the fields in this document.
+ let projectPipe = [{
+ $project: {
+ "2i": 1,
+ "3i": 1,
+ "6i^2": {"$multiply": ["$2i", "$3i"]},
+ // _id is implicitly included.
+ }
+ }];
+ let correct = coll.aggregate(projectPipe).toArray();
- // Then assert they are the same.
- assert(arrayEq(addFieldsResult, correct),
- "$addFields does not work the same as a $project with computed and included fields");
- }
+ // Then compute the same results using $addFields.
+ let addFieldsPipe = [{
+ $addFields: {
+ "6i^2": {"$multiply": ["$2i", "$3i"]},
+ // All other fields are implicitly included.
+ }
+ }];
+ let addFieldsResult = coll.aggregate(addFieldsPipe).toArray();
- // Test against the standalone started by resmoke.py.
- let conn = db.getMongo();
- doExecutionTest(conn);
- print("Success! Standalone execution use case test for $addFields passed.");
+ // Then assert they are the same.
+ assert(arrayEq(addFieldsResult, correct),
+ "$addFields does not work the same as a $project with computed and included fields");
+}
- // Test against a sharded cluster.
- let st = new ShardingTest({shards: 2});
- doExecutionTest(st.s0);
- st.stop();
- print("Success! Sharding use case test for $addFields passed.");
+// Test against the standalone started by resmoke.py.
+let conn = db.getMongo();
+doExecutionTest(conn);
+print("Success! Standalone execution use case test for $addFields passed.");
+// Test against a sharded cluster.
+let st = new ShardingTest({shards: 2});
+doExecutionTest(st.s0);
+st.stop();
+print("Success! Sharding use case test for $addFields passed.");
}());