diff options
author | Benety Goh <benety@mongodb.com> | 2021-11-19 18:34:21 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-11-19 19:06:49 +0000 |
commit | 4f5d04da22f664c436da9f31be03e42c7d19d827 (patch) | |
tree | 429187d2808d6a3e46ed784389b4a6b80148e180 /jstests/core/countc.js | |
parent | ebef2399f1672f50d4990697ee1338c39aa32ed6 (diff) | |
download | mongo-4f5d04da22f664c436da9f31be03e42c7d19d827.tar.gz |
SERVER-61636 clean up countc.js
Diffstat (limited to 'jstests/core/countc.js')
-rw-r--r-- | jstests/core/countc.js | 92 |
1 files changed, 55 insertions, 37 deletions
diff --git a/jstests/core/countc.js b/jstests/core/countc.js index 68986f9424f..4afa7976931 100644 --- a/jstests/core/countc.js +++ b/jstests/core/countc.js @@ -17,13 +17,17 @@ let t = db.getCollection(collNamePrefix + collCount++); t.drop(); // Match a subset of inserted values within a $in operator. -t.createIndex({a: 1}); +assert.commandWorked(t.createIndex({a: 1})); // Save 'a' values 0, 0.5, 1.5, 2.5 ... 97.5, 98.5, 99. -t.save({a: 0}); -t.save({a: 99}); +let docs = []; +let docId = 0; +docs.push({_id: docId++, a: 0}); +docs.push({_id: docId++, a: 99}); for (let i = 0; i < 99; ++i) { - t.save({a: (i + 0.5)}); + docs.push({_id: docId++, a: (i + 0.5)}); } +assert.commandWorked(t.insert(docs)); + // Query 'a' values $in 0, 1, 2, ..., 99. let vals = []; for (let i = 0; i < 100; ++i) { @@ -35,53 +39,61 @@ assert.eq(2, t.count({a: {$in: vals}})); // Match 'a' values within upper and lower limits. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: 1}); -t.save({a: [1, 2]}); // Will match because 'a' is in range. -t.save({a: 9}); // Will not match because 'a' is not in range. +assert.commandWorked(t.createIndex({a: 1})); +assert.commandWorked(t.insert([ + {_id: docId++, a: [1, 2]}, // Will match because 'a' is in range. + {_id: docId++, a: 9}, // Will not match because 'a' is not in range. +])); // Only one document matches. assert.eq(1, t.count({a: {$gt: 0, $lt: 5}})); // Match two nested fields within an array. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({'a.b': 1, 'a.c': 1}); -t.save({a: [{b: 2, c: 3}, {}]}); +assert.commandWorked(t.createIndex({'a.b': 1, 'a.c': 1})); +assert.commandWorked(t.insert({a: [{b: 2, c: 3}, {}]})); // The document does not match because its c value is 3. assert.eq(0, t.count({'a.b': 2, 'a.c': 2})); // $gt:string only matches strings. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: 1}); -t.save({a: 'a'}); // Will match. -t.save({a: {}}); // Will not match because {} is not a string. +assert.commandWorked(t.createIndex({a: 1})); +assert.commandWorked(t.insert([ + {_id: docId++, a: 'a'}, // Will match. + {_id: docId++, a: {}}, // Will not match because {} is not a string. +])); // Only one document matches. assert.eq(1, t.count({a: {$gte: ''}})); // $lte:date only matches dates. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: 1}); -t.save({a: new Date(1)}); // Will match. -t.save({a: true}); // Will not match because 'true' is not a date. +assert.commandWorked(t.createIndex({a: 1})); +assert.commandWorked(t.insert([ + {_id: docId++, a: new Date(1)}, // Will match. + {_id: docId++, a: true}, // Will not match because 'true' is not a date. +])); // Only one document matches. assert.eq(1, t.count({a: {$lte: new Date(1)}})); // Querying for 'undefined' triggers an error. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: 1}); -assert.throws(function() { +assert.commandWorked(t.createIndex({a: 1})); +assert.throwsWithCode(function() { t.count({a: undefined}); -}); +}, ErrorCodes.BadValue); // Count using a descending order index. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: -1}); -t.save({a: 1}); -t.save({a: 2}); -t.save({a: 3}); +assert.commandWorked(t.createIndex({a: -1})); +assert.commandWorked(t.insert([ + {_id: docId++, a: 1}, + {_id: docId++, a: 2}, + {_id: docId++, a: 3}, +])); assert.eq(1, t.count({a: {$gt: 2}})); assert.eq(1, t.count({a: {$lt: 2}})); assert.eq(2, t.count({a: {$lte: 2}})); @@ -90,11 +102,13 @@ assert.eq(2, t.count({a: {$lt: 3}})); // Count using a compound index. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: 1, b: 1}); -t.save({a: 1, b: 2}); -t.save({a: 2, b: 1}); -t.save({a: 2, b: 3}); -t.save({a: 3, b: 4}); +assert.commandWorked(t.createIndex({a: 1, b: 1})); +assert.commandWorked(t.insert([ + {_id: docId++, a: 1, b: 2}, + {_id: docId++, a: 2, b: 1}, + {_id: docId++, a: 2, b: 3}, + {_id: docId++, a: 3, b: 4}, +])); assert.eq(1, t.count({a: 1})); assert.eq(2, t.count({a: 2})); assert.eq(1, t.count({a: {$gt: 2}})); @@ -106,11 +120,13 @@ assert.eq(1, t.count({a: 1, b: {$lt: 3}})); // Count using a compound descending order index. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: 1, b: -1}); -t.save({a: 1, b: 2}); -t.save({a: 2, b: 1}); -t.save({a: 2, b: 3}); -t.save({a: 3, b: 4}); +assert.commandWorked(t.createIndex({a: 1, b: -1})); +assert.commandWorked(t.insert([ + {_id: docId++, a: 1, b: 2}, + {_id: docId++, a: 2, b: 1}, + {_id: docId++, a: 2, b: 3}, + {_id: docId++, a: 3, b: 4}, +])); assert.eq(1, t.count({a: {$gt: 2}})); assert.eq(1, t.count({a: {$lt: 2}})); assert.eq(2, t.count({a: 2, b: {$gt: 0}})); @@ -120,15 +136,17 @@ assert.eq(1, t.count({a: 1, b: {$lt: 3}})); // Count with a multikey value. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: 1}); -t.save({a: [1, 2]}); +assert.commandWorked(t.createIndex({a: 1})); +assert.commandWorked(t.insert({_id: docId++, a: [1, 2]})); assert.eq(1, t.count({a: {$gt: 0, $lte: 2}})); // Count with a match constraint on an unindexed field. t = db.getCollection(collNamePrefix + collCount++); t.drop(); -t.createIndex({a: 1}); -t.save({a: 1, b: 1}); -t.save({a: 1, b: 2}); +assert.commandWorked(t.createIndex({a: 1})); +assert.commandWorked(t.insert([ + {_id: docId++, a: 1, b: 1}, + {_id: docId++, a: 1, b: 2}, +])); assert.eq(1, t.count({a: 1, $where: 'this.b == 1'})); })(); |