summaryrefslogtreecommitdiff
path: root/jstests/count6.js
blob: 44c5fa33bc77343be51d41266ba39348a5c9839a (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
// Some correctness checks for fast and normal count modes, including with skip and limit.

t = db.jstests_count6;

function checkCountForObject( obj ) {
    t.drop();
    t.ensureIndex( {b:1,a:1} );
    
    function checkCounts( query, expected ) {
        assert.eq( expected, t.count( query ) , "A1" );
        assert.eq( expected, t.find( query ).skip( 0 ).limit( 0 ).count( true ) , "A2" );
        // Check proper counts with various skip and limit specs.
        for( var skip = 1; skip <= 2; ++skip ) {
            for( var limit = 1; limit <= 2; ++limit ) {
                assert.eq( Math.max( expected - skip, 0 ), t.find( query ).skip( skip ).count( true ) , "B1" );
                assert.eq( Math.min( expected, limit ), t.find( query ).limit( limit ).count( true ) , "B2" );
                assert.eq( Math.min( Math.max( expected - skip, 0 ), limit ), t.find( query ).skip( skip ).limit( limit ).count( true ) , "B4" );

                // Check limit(x) = limit(-x)
                assert.eq( t.find( query ).limit( limit ).count( true ),
                           t.find( query ).limit( -limit ).count( true ) , "C1" );
                assert.eq( t.find( query ).skip( skip ).limit( limit ).count( true ),
                           t.find( query ).skip( skip ).limit( -limit ).count( true ) , "C2" );
            }
        }

        // Check limit(0) has no effect
        assert.eq( expected, t.find( query ).limit( 0 ).count( true ) , "D1" );
        assert.eq( Math.max( expected - skip, 0 ),
                   t.find( query ).skip( skip ).limit( 0 ).count( true ) , "D2" );
        assert.eq( expected, t.getDB().runCommand({ count: t.getName(),
                        query: query, limit: 0 }).n , "D3" );
        assert.eq( Math.max( expected - skip, 0 ),
                   t.getDB().runCommand({ count: t.getName(),
                               query: query, limit: 0, skip: skip }).n , "D4" );
    }

    for( var i = 0; i < 5; ++i ) {
        checkCounts( {a:obj.a,b:obj.b}, i );
        checkCounts( {b:obj.b,a:obj.a}, i );
        t.insert( obj );    
    }

    t.insert( {a:true,b:true} );
    t.insert( {a:true,b:1} );
    t.insert( {a:false,b:1} );
    t.insert( {a:false,b:true} );
    t.insert( {a:false,b:false} );

    checkCounts( {a:obj.a,b:obj.b}, i );
    checkCounts( {b:obj.b,a:obj.a}, i );

    // Check with no query
    checkCounts( {}, 10 );
}

// Check fast count mode.
checkCountForObject( {a:true,b:false} );

// Check normal count mode.
checkCountForObject( {a:1,b:0} );