summaryrefslogtreecommitdiff
path: root/jstests/core/index/index_bounds_object.js
blob: 255fb55f117dcbd2e4f1dfd737cdc786470018ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Index bounds generation tests for Object values.
// @tags: [
//   assumes_unsharded_collection,
//   requires_non_retryable_writes,
// ]
(function() {
"use strict";

load("jstests/libs/analyze_plan.js");  // For assertCoveredQueryAndCount.

const coll = db.index_bounds_object;
coll.drop();

assert.commandWorked(coll.createIndex({a: 1}));
assert.commandWorked(coll.insert({a: {b: 1}}));

// Test that queries involving comparison operators with objects are covered.
const proj = {
    a: 1,
    _id: 0
};
assertCoveredQueryAndCount({collection: coll, query: {a: {$gt: {b: 0}}}, project: proj, count: 1});
assertCoveredQueryAndCount({collection: coll, query: {a: {$gt: {b: 2}}}, project: proj, count: 0});
assertCoveredQueryAndCount({collection: coll, query: {a: {$gte: {b: 1}}}, project: proj, count: 1});
assertCoveredQueryAndCount(
    {collection: coll, query: {a: {$gte: {b: 1, c: 2}}}, project: proj, count: 0});
assertCoveredQueryAndCount({collection: coll, query: {a: {$lt: {b: 2}}}, project: proj, count: 1});
assertCoveredQueryAndCount({collection: coll, query: {a: {$lte: {b: 1}}}, project: proj, count: 1});

// Test that queries involving comparisons with an empty object are covered.
assert.commandWorked(coll.insert({a: {}}));
assertCoveredQueryAndCount({collection: coll, query: {a: {$gt: {}}}, project: proj, count: 1});
assertCoveredQueryAndCount({collection: coll, query: {a: {$gte: {}}}, project: proj, count: 2});
assertCoveredQueryAndCount({collection: coll, query: {a: {$lt: {}}}, project: proj, count: 0});
assertCoveredQueryAndCount({collection: coll, query: {a: {$lte: {}}}, project: proj, count: 1});

// Test that queries involving comparisons with a range of objects are covered.
assertCoveredQueryAndCount(
    {collection: coll, query: {a: {$gt: {}, $lt: {b: 2}}}, project: proj, count: 1});
assertCoveredQueryAndCount(
    {collection: coll, query: {a: {$gte: {}, $lt: {b: 2}}}, project: proj, count: 2});
assertCoveredQueryAndCount(
    {collection: coll, query: {a: {$lt: {}, $gte: {}}}, project: proj, count: 0});

// Test that documents that lie outside of the generated index bounds are not returned. Cannot
// test empty array upper bounds since that would force the index to be multi-key.
coll.remove({});
assert.commandWorked(coll.insert({a: "string"}));
assert.commandWorked(coll.insert({a: true}));
assertCoveredQueryAndCount({collection: coll, query: {a: {$gt: {}}}, project: proj, count: 0});
assertCoveredQueryAndCount({collection: coll, query: {a: {$gte: {}}}, project: proj, count: 0});
assertCoveredQueryAndCount({collection: coll, query: {a: {$lt: {}}}, project: proj, count: 0});
assertCoveredQueryAndCount({collection: coll, query: {a: {$lte: {}}}, project: proj, count: 0});

// Adding a document containing an array makes the index multi-key which can never be used for a
// covered query.
assert.commandWorked(coll.insert({a: []}));
assert(!isIndexOnly(db, getWinningPlan(coll.find({a: {$gt: {}}}, proj).explain().queryPlanner)));
assert(!isIndexOnly(db, getWinningPlan(coll.find({a: {$gte: {}}}, proj).explain().queryPlanner)));
assert(!isIndexOnly(db, getWinningPlan(coll.find({a: {$lt: {}}}, proj).explain().queryPlanner)));
assert(!isIndexOnly(db, getWinningPlan(coll.find({a: {$lte: {}}}, proj).explain().queryPlanner)));
})();