summaryrefslogtreecommitdiff
path: root/jstests/arrayfind2.js
blob: 1e63bf63239ef49d911b55d70060cb25395f1779 (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

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

function go( prefix ){
    assert.eq( 3 , t.count() , prefix + " A1" );
    assert.eq( 3 , t.find( { a : { $elemMatch : { x : { $gt : 4 } } } } ).count() , prefix + " A2" );
    assert.eq( 1 , t.find( { a : { $elemMatch : { x : { $lt : 2 } } } } ).count() , prefix + " A3" );
    assert.eq( 1 , t.find( { a : { $all : [ { $elemMatch : { x : { $lt : 4 } } } ,
                                            { $elemMatch : { x : { $gt : 5 } } } ] } } ).count() , prefix + " A4" );
    
    assert.throws( function() { return t.findOne( { a : { $all : [ 1, { $elemMatch : { x : 3 } } ] } } ) } );
    assert.throws( function() { return t.findOne( { a : { $all : [ /a/, { $elemMatch : { x : 3 } } ] } } ) } );
        
}

t.save( { a : [ { x : 1 } , { x : 5 } ] } )
t.save( { a : [ { x : 3 } , { x : 5 } ] } )
t.save( { a : [ { x : 3 } , { x : 6 } ] } )

go( "no index" );
t.ensureIndex( { a : 1 } );
go( "index(a)" );

assert.eq( {}, t.find( { a : { $all : [ { $elemMatch : { x : 3 } } ] } } ).explain().indexBounds );

t.ensureIndex( { "a.x": 1 } );

assert.eq( {"a.x":[[3,3]]}, t.find( { a : { $all : [ { $elemMatch : { x : 3 } } ] } } ).explain().indexBounds );
// only first $elemMatch used to find bounds
assert.eq( {"a.x":[[3,3]]}, t.find( { a : { $all : [ { $elemMatch : { x : 3 } }, { $elemMatch : { y : 5 } } ] } } ).explain().indexBounds );

t.ensureIndex( { "a.x":1,"a.y":-1 } );

// TODO Index bounds below for elemMatch could be improved. - SERVER-3104
assert.eq( {"a.x":[[3,3]],"a.y":[[{$maxElement:1},{$minElement:1}]]}, t.find( { a : { $all : [ { $elemMatch : { x : 3, y : { $gt: 4 } } } ] } } ).explain().indexBounds );