summaryrefslogtreecommitdiff
path: root/jstests/indexn.js
blob: 9abb001eed91f70e63023edd70937afbd295547b (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
// Test "impossible match" queries, or queries that will always have
// an empty result set.

t = db.jstests_indexn;
t.drop();

function checkImpossibleMatch( explain ) {
    printjson(explain);
    assert.eq( 0, explain.n );
}

t.save( {a:1,b:[1,2]} );

t.ensureIndex( {a:1} );
t.ensureIndex( {b:1} );

// {a:1} is a single key index, so no matches are possible for this query
assert.eq( 0, t.count( {a:{$gt:5,$lt:0}} ) );
checkImpossibleMatch( t.find( {a:{$gt:5,$lt:0}} ).explain() );

assert.eq( 0, t.count( {a:{$gt:5,$lt:0},b:2} ) );
checkImpossibleMatch( t.find( {a:{$gt:5,$lt:0},b:2} ).explain() );

assert.eq( 0, t.count( {a:{$gt:5,$lt:0},b:{$gt:0,$lt:5}} ) );
checkImpossibleMatch( t.find( {a:{$gt:5,$lt:0},b:{$gt:0,$lt:5}} ).explain() );

// One clause of an $or is an "impossible match"
printjson( t.find( {$or:[{a:{$gt:5,$lt:0}},{a:1}]} ).explain() )
assert.eq( 1, t.count( {$or:[{a:{$gt:5,$lt:0}},{a:1}]} ) );
checkImpossibleMatch( t.find( {$or:[{a:{$gt:5,$lt:0}},{a:1}]} ).explain().clauses[ 0 ] );

// One clause of an $or is an "impossible match"; original order of the $or
// does not matter.
printjson( t.find( {$or:[{a:1},{a:{$gt:5,$lt:0}}]} ).explain() )
assert.eq( 1, t.count( {$or:[{a:1},{a:{$gt:5,$lt:0}}]} ) );
checkImpossibleMatch( t.find( {$or:[{a:1},{a:{$gt:5,$lt:0}}]} ).explain().clauses[ 0 ] );

t.save( {a:2} );

// Descriptive test: query system sees this query as an $or where
// one clause of the $or is an $and. The $and bounds get intersected
// forming a clause with empty index bounds. The union of the $or bounds
// produces the two point intervals [1, 1] and [2, 2].
assert.eq( 2, t.count( {$or:[{a:1},{a:{$gt:5,$lt:0}},{a:2}]} ) );
explain = t.find( {$or:[{a:1},{a:{$gt:5,$lt:0}},{a:2}]} ).explain();
printjson( explain )
assert.eq( 2, explain.clauses.length );
checkImpossibleMatch( explain.clauses[ 0 ] );
assert.eq( [[1, 1], [2,2]], explain.clauses[ 1 ].indexBounds.a );