summaryrefslogtreecommitdiff
path: root/jstests/arrayfind7.js
blob: d68527a62431400ed0a2f09e6bc45f56e65245b6 (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
63
64
65
66
67
68
69
// Nested $elemMatch clauses.  SERVER-5741

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

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

function checkElemMatchMatches() {
    assert.eq( 1, t.count( { a:{ $elemMatch:{ b:{ $elemMatch:{ c:1, d:2 } } } } } ) );
}

// The document is matched using nested $elemMatch expressions, with and without an index.
checkElemMatchMatches();
t.ensureIndex( { 'a.b.c':1 } );
checkElemMatchMatches();

function checkIndexCharacterBasedBounds( index, document, query, singleKeyBounds, multiKeyBounds ) {
    // The document is matched without an index, and with single and multi key indexes.
    t.drop();
    t.save( document );
    assert.eq( 1, t.count( query ) );
    t.ensureIndex( index );
    assert.eq( 1, t.count( query ) );
    t.save( { a:{ b:{ c:[ 10, 11 ] } } } ); // Make the index multikey.
    assert.eq( 1, t.count( query ) );

    // The single and multi key index bounds are as expected.
    t.drop();
    t.ensureIndex( index );
    assert.eq( singleKeyBounds, t.find( query ).explain().indexBounds[ 'a.b.c' ] );
    t.save( { a:{ b:{ c:[ 10, 11 ] } } } );
    assert.eq( multiKeyBounds, t.find( query ).explain().indexBounds[ 'a.b.c' ] );
}

// Two constraints within a nested $elemMatch expression.
checkIndexCharacterBasedBounds( { 'a.b.c':1 },
                               { a:[ { b:[ { c:1 } ] } ] },
                               { a:{ $elemMatch:{ b:{ $elemMatch:{ c:{ $gte:1, $lte:1 } } } } } },
                               [ [ 1, 1 ] ],
                               [ [ 1, 1.7976931348623157e+308 ] ] );

// Two constraints within a nested $elemMatch expression, one of which contains the other.
checkIndexCharacterBasedBounds( { 'a.b.c':1 },
                               { a:[ { b:[ { c:2 } ] } ] },
                               { a:{ $elemMatch:{ b:{ $elemMatch:{ c:{ $gte:1, $in:[2] } } } } } },
                               [ [ 2, 2 ] ],
                               [ [ 2, 2 ] ] );

// Two nested $elemMatch expressions.
checkIndexCharacterBasedBounds( { 'a.d.e':1, 'a.b.c':1 },
                               { a:[ { b:[ { c:1 } ], d:[ { e:1 } ] } ] },
                               { a:{ $elemMatch:{ d:{ $elemMatch:{ e:{ $lte:1 } } },
                                    b:{ $elemMatch:{ c:{ $gte:1 } } } } } },
                               [ [ 1, 1.7976931348623157e+308 ] ],
                               [ [ { $minElement:1 }, { $maxElement:1 } ] ] );

// A non $elemMatch expression and a nested $elemMatch expression.
checkIndexCharacterBasedBounds( { 'a.x':1, 'a.b.c':1 },
                               { a:[ { b:[ { c:1 } ], x:1 } ] },
                               { 'a.x':1, a:{ $elemMatch:{ b:{ $elemMatch:{ c:{ $gte:1 } } } } } },
                               [ [ 1, 1.7976931348623157e+308 ] ],
                               [ [ { $minElement:1 }, { $maxElement:1 } ] ] );

// $elemMatch is applied directly to a top level field.
checkIndexCharacterBasedBounds( { 'a.b.c':1 },
                               { a:[ { b:[ { c:[ 1 ] } ] } ] },
                               { a:{ $elemMatch:{ 'b.c':{ $elemMatch:{ $gte:1, $lte:1 } } } } },
                               [ [ 1, 1 ] ],
                               [ [ 1, 1 ] ] );