diff options
Diffstat (limited to 'src/mongo/db/query/canonical_query_encoder_test.cpp')
-rw-r--r-- | src/mongo/db/query/canonical_query_encoder_test.cpp | 68 |
1 files changed, 43 insertions, 25 deletions
diff --git a/src/mongo/db/query/canonical_query_encoder_test.cpp b/src/mongo/db/query/canonical_query_encoder_test.cpp index 4086f64402f..1d647910ce8 100644 --- a/src/mongo/db/query/canonical_query_encoder_test.cpp +++ b/src/mongo/db/query/canonical_query_encoder_test.cpp @@ -123,50 +123,68 @@ TEST(CanonicalQueryEncoderTest, ComputeKey) { // With sort testComputeKey("{}", "{a: 1}", "{}", "an~aa"); testComputeKey("{}", "{a: -1}", "{}", "an~da"); - testComputeKey("{}", - "{a: {$meta: 'textScore'}}", - "{a: {$meta: 'textScore'}}", - "an~ta|{ $meta: \"textScore\" }a"); + testComputeKey("{}", "{a: {$meta: 'textScore'}}", "{a: {$meta: 'textScore'}}", "an~ta"); testComputeKey("{a: 1}", "{b: 1}", "{}", "eqa~ab"); // With projection - testComputeKey("{}", "{}", "{a: 1}", "an|ia"); - testComputeKey("{}", "{}", "{a: -1}", "an|ia"); - testComputeKey("{}", "{}", "{a: -1.0}", "an|ia"); - testComputeKey("{}", "{}", "{a: true}", "an|ia"); - testComputeKey("{}", "{}", "{a: 0}", "an|ea"); - testComputeKey("{}", "{}", "{a: false}", "an|ea"); - testComputeKey("{}", "{}", "{a: 99}", "an|ia"); - testComputeKey("{}", "{}", "{a: 'foo'}", "an|ia"); - testComputeKey("{}", "{}", "{a: {$slice: [3, 5]}}", "an|{ $slice: \\[ 3\\, 5 \\] }a"); - testComputeKey("{}", "{}", "{a: {$elemMatch: {x: 2}}}", "an|{ $elemMatch: { x: 2 } }a"); - testComputeKey("{}", "{}", "{a: ObjectId('507f191e810c19729de860ea')}", "an|ia"); - testComputeKey("{a: 1}", "{}", "{'a.$': 1}", "eqa|ia.$"); - testComputeKey("{a: 1}", "{}", "{a: 1}", "eqa|ia"); + testComputeKey("{}", "{}", "{a: 1}", "an|_id-a"); + testComputeKey("{}", "{}", "{a: -1}", "an|_id-a"); + testComputeKey("{}", "{}", "{a: -1.0}", "an|_id-a"); + testComputeKey("{}", "{}", "{a: true}", "an|_id-a"); + testComputeKey("{}", "{}", "{a: 0}", "an"); + testComputeKey("{}", "{}", "{a: false}", "an"); + testComputeKey("{}", "{}", "{a: 99}", "an|_id-a"); + testComputeKey("{}", "{}", "{a: 'foo'}", "an|_id-a"); + // $slice defaults to exclusion. + testComputeKey("{}", "{}", "{a: {$slice: [3, 5]}}", "an"); + testComputeKey("{}", "{}", "{a: {$slice: [3, 5]}, b: 0}", "an"); + + // But even when using $slice in an inclusion, the entire document is needed. + testComputeKey("{}", "{}", "{a: {$slice: [3, 5]}, b: 1}", "an"); + + testComputeKey("{}", "{}", "{a: {$elemMatch: {x: 2}}}", "an"); + testComputeKey("{}", "{}", "{a: {$elemMatch: {x: 2}}, b: 0}", "an"); + testComputeKey("{}", "{}", "{a: {$elemMatch: {x: 2}}, b: 1}", "an"); + + testComputeKey("{}", "{}", "{a: {$slice: [3, 5]}, b: {$elemMatch: {x: 2}}}", "an"); + + testComputeKey("{}", "{}", "{a: ObjectId('507f191e810c19729de860ea')}", "an|_id-a"); + testComputeKey("{a: 1}", "{}", "{'a.$': 1}", "eqa"); + testComputeKey("{a: 1}", "{}", "{a: 1}", "eqa|_id-a"); // Projection should be order-insensitive - testComputeKey("{}", "{}", "{a: 1, b: 1}", "an|iaib"); - testComputeKey("{}", "{}", "{b: 1, a: 1}", "an|iaib"); + testComputeKey("{}", "{}", "{a: 1, b: 1}", "an|_id-a-b"); + testComputeKey("{}", "{}", "{b: 1, a: 1}", "an|_id-a-b"); + + // And should escape the separation character. + testComputeKey("{}", "{}", "{'b-1': 1, 'a-2': 1}", "an|_id-a\\-2-b\\-1"); + + // And should exclude $-prefixed fields which can be added internally. + testComputeKey("{}", "{x: 1}", "{$sortKey: {$meta: 'sortKey'}}", "an~ax"); + testComputeKey("{}", "{}", "{}", "an"); + + testComputeKey("{}", "{x: 1}", "{a: 1, $sortKey: {$meta: 'sortKey'}}", "an~ax|_id-a"); + testComputeKey("{}", "{}", "{a: 1}", "an|_id-a"); // With or-elimination and projection - testComputeKey("{$or: [{a: 1}]}", "{}", "{_id: 0, a: 1}", "eqa|e_idia"); - testComputeKey("{$or: [{a: 1}]}", "{}", "{'a.$': 1}", "eqa|ia.$"); + testComputeKey("{$or: [{a: 1}]}", "{}", "{_id: 0, a: 1}", "eqa|a"); + testComputeKey("{$or: [{a: 1}]}", "{}", "{'a.$': 1}", "eqa"); } // Delimiters found in user field names or non-standard projection field values // must be escaped. TEST(CanonicalQueryEncoderTest, ComputeKeyEscaped) { // Field name in query. - testComputeKey("{'a,[]~|<>': 1}", "{}", "{}", "eqa\\,\\[\\]\\~\\|<>"); + testComputeKey("{'a,[]~|-<>': 1}", "{}", "{}", "eqa\\,\\[\\]\\~\\|\\-<>"); // Field name in sort. - testComputeKey("{}", "{'a,[]~|<>': 1}", "{}", "an~aa\\,\\[\\]\\~\\|<>"); + testComputeKey("{}", "{'a,[]~|-<>': 1}", "{}", "an~aa\\,\\[\\]\\~\\|\\-<>"); // Field name in projection. - testComputeKey("{}", "{}", "{'a,[]~|<>': 1}", "an|ia\\,\\[\\]\\~\\|<>"); + testComputeKey("{}", "{}", "{'a,[]~|-<>': 1}", "an|_id-a\\,\\[\\]\\~\\|\\-<>"); // Value in projection. - testComputeKey("{}", "{}", "{a: 'foo,[]~|<>'}", "an|ia"); + testComputeKey("{}", "{}", "{a: 'foo,[]~|-<>'}", "an|_id-a"); } // Cache keys for $geoWithin queries with legacy and GeoJSON coordinates should |